9.2 Additional Control Statements | 439
and continue with Statement5. If digitis none of the values
previously mentioned, execute Statement4 and continue
with Statement5.” Figure 9.2 shows the flow of control
through this statement.
The breakstatement causes control to immediately jump
to the statement following the switchstatement. We will see
shortly what happens if we omit the breakstatements.
Let’s look at the syntax template for the switchstate-
ment and then consider what actually happens when it ex-
ecutes. The syntax template for the switchstatement is
Here Integral-Expression is an expression of type char,byte,short, or int. The Switch-Label
in front of a statement is either a case labelor a default label:
In a case label, Constant-Expression is an expression of type char,byte,short, or int,
whose operands must be literal or named constants. Following are examples of constant ex-
pressions (where CLASS_SIZEis a named constant of type int):
3
CLASS_SIZE
'A'
The data type of Constant-Expression is converted, if necessary, to match the type of the
switch expression.
In our earlier example that tests the value of digit, the following are the case labels:
case1 :
case2 :
case3 :
case4 :
Switch-Label
case Constant-Expression :
default :
Switch-Statement
switch (Integral-Expression)
{
SwitchLabel.. .Statement....
..
}
If digit == 1
If digit == 2
or digit == 3
If digit == 4
default
Statement1; break;
Statement2; break;
Statement3; break;
Statement4;
Statement5;
Figure 9.2 Flow of Control in the Example switch
Statement