Switch Case: (Selection Statement)
Switch Case: (Selection Statement)
(SELECTION STATEMENT)
INTRODUCTION
On finding a matching value the control jumps to the statement pertaining to that value
and the statement is executed, till the break statement is encountered or the end of switch
is reached.
The expression must either evaluate to an integer value or a character value. It
SYNTAX:
switch( variable / expression )
{
case value1: statements1;
break;
case value2: statements2;
break;
..
default: statements3;
} EG1
EG2
When a match is found, the statement sequence associated with that case is executed until
DEFAULT CLAUSE
fail.
Stop the execution of the switch statement and transfer the control to the statement
statements from matching case and keep on executing statements for following
cases as well until either a break statement is found or switch statements end is
encountered.
Fall through
code following the switch statement i.e., outside the body of switch statement.
No two case labels in the same switch can have identical values
switch(dow)
{
case 0:
ans=Sunday;break;
case 1: ans=Monday;break;
case 2: ans=Tuesday;break;
case 3: ans=Wednesday;break;
case 4: ans= Thursday;break;
case 5: ans=Friday;break;
case 6: ans=Saturday; break;
default: ans=invalid input ;
}
outputLabel.setText(Weekday for day no: +dow+ is +ans);
Syntax
:
QUESTIONS
1) day finder
2) Week of the day
3) Calculator program
4) Area of shape
Create an application that obtains day (1-31), month (1-12), and year (>0) from the user
and displays in dd -MON- yyyy format, e.g., if the user has entered as 13,04,2009 as day ,
month and year then it display date as 13-APR-2009.