Concepts of Programming Languages

(Sean Pound) #1

356 Chapter 8 Statement-Level Control Structures


case 3: odd += 1;
sumodd += index;
case 2:
case 4: even += 1;
sumeven += index;
default: printf("Error in switch, index = %d\n", index);
}

This code prints the error message on every execution. Likewise, the code for
the 2 and 4 constants is executed every time the code at the 1 or 3 constants
is executed. To separate these segments logically, an explicit branch must be
included. The break statement, which is actually a restricted goto, is normally
used for exiting switch statements.
The following switch statement uses break to restrict each execution to
a single selectable segment:

switch (index) {
case 1:
case 3: odd += 1;
sumodd += index;
break;
case 2:
case 4: even += 1;
sumeven += index;
break;
default: printf("Error in switch, index = %d\n", index);
}

Occasionally, it is convenient to allow control to flow from one selectable
code segment to another. For example, in the example above, the segments for
the case values 1 and 2 are empty, allowing control to flow to the segments for
3 and 4 , respectively. This is the reason why there are no implicit branches in
the switch statement. The reliability problem with this design arises when the
mistaken absence of a break statement in a segment allows control to flow to
the next segment incorrectly. The designers of C’s switch traded a decrease
in reliability for an increase in flexibility. Studies have shown, however, that the
ability to have control flow from one selectable segment to another is rarely
used. C’s switch is modeled on the multiple-selection statement in ALGOL
68, which also does not have implicit branches from selectable segments.
The C switch statement has virtually no restrictions on the placement of
the case expressions, which are treated as if they were normal statement labels.
This laxness can result in highly complex structure within the switch body. The
following example is taken from Harbison and Steele (2002).

switch (x)
default:
if (prime(x))
Free download pdf