2.10 SWITCH AND BREAK
C provides an extremely handy
switch-case construct
to facilitate selecting between multiple
alternatives. Within the block of code controlled by a
switch statement,
the programmer can place
case statements
and (optionally) a
default statement. For example,
switch (x) {
case 0 : str = “none”; break;
case 1 :
str = “single” ; break;
case 2 :
str = “pair” ;
break;
default : str = “many” ;
}
Each case keyword is followed by an integer constant, followed by a colon.
Please note that the
code block belonging to each
case
can immediately follow the colon on the same line or on
separate lines.
If the value of x is not one of the values provided for in case statements, the
default
statement
code is executed.
Also note that if there is no default statement and no exact match,
execution resumes after the
switch
block of code.
In the program example above, break statements are used that terminate the code for each
case
and
cause execution to continue after the
Do'stlaringiz bilan baham: