Figure 5.12 shows the UML activity diagram for the general switch
multiple-selection statement. Most switch
statements use a break
in each case
to terminate the switch
statement after processing the case
. Figure 5.12 emphasizes this by including break
statements in the activity diagram. Without the break
statement, control would not transfer to the first statement after the switch
statement after a case
is processed. Instead, control would transfer to the next case
’s actions.
The diagram makes it clear that the break
statement at the end of a case
causes control to exit the switch
statement immediately. Again, note that (besides an initial state, transition arrows, a final state and several notes) the diagram contains action states and decisions. Also, the diagram uses merge symbols to merge the transitions from the break
statements to the final state.
Each case
can be used to test only an integral constant expression—any combination of character constants and integer constants that evaluates to a constant integer value. A character constant is represented as the specific character in single quotes, such as 'A'
. An integer constant is simply an integer value. Also, each case
label can specify only one integral constant expression.
Common Programming Error 5.7
Providing case
labels with identical values in a switch statement is a compilation error.