Decision Statements 101
Table 5.1 Difference between break and continue.
Break Continue
Exits from current block or loop.
Loop takes next iteration.
Control passes to next statement.
Control passes at the beginning of loop.
Terminates the program.
Never terminates the program.
5.8 THE switch STATEMENT
The sw itch statement is a multi-way branch statement. In the program if there is a possibility to
make a choice from a number of options, this structured selection is useful. The sw itch statement
requires only one argument of any data type, which is checked with number of ca s e options. The
switch statement evaluates expression and then looks for its value among the case constants. If the
value matches with cas e constant, this particular ca se statement is executed. If not, d e fa u lt is
executed. Here sw itch , ca se and d e fa u lt are reserved keywords. Every ca se statement
terminates withThe break statement is used to exit from the current ca se structure. The sw itch
() statement is useful for writing menu driven program.
The syntax of the sw itch () case statement is as under.
switch(variable or expression)
{
case constant A :
statement;
break;
case constant B :
statement;
break;
default:
statement;
1
a)The switch () expression In the block the variable or expression can be a character or an integer.
The integer expression following the keyword sw itch () will yield an integer value only. The
integer may be any value 1,2,3 etc. In case of character constant the values may be with alphabets
such as ' x ' , ' y ' , 'z ' etc.
b) The switch () organization The sw itch () expression should neither be terminated with (;)
semi-colon nor with any other symbol. The entire case structure following sw itch () should be
enclosed with curly braces. The keyword case is followed by a constant. Every constant terminates
with a colon (:). Each case statement must contain different constant values. Any number of c ase
statements can be provided. If the case structure contains multiple statements, they need not be
enclosed within curly braces. Here, the keyword ca s e & break performs respectively the job of
opening and closing curly braces.