![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Rewriting the code fragment shown in Segment 416, using a
switch
statement, improves clarity:
switch (industry) { case 1: case 5: trade_pointers[limit] = (struct trade*) malloc (sizeof (struct trade)); trade_pointers[limit] -> price = price; trade_pointers[limit] -> number = number; ++limit; break; case 0: case 2: case 3: case 4: break; default: printf ("Industry code %i is unknown!\n", industry); }
If the industry code is 1
, execution begins just after
case 1
. Because there are no statements before case 5
, you
might think that nothing would happen, but no statements implies no
break
statement, so execution falls through to the several
statements, terminated by a break
statement, following
case 5
.
Thus, trades with an industry code of 1
or 5
lead to the
execution of the same statements. Codes 0
, 2
, 3
, and
4
all lead to the execution of no statements. Other codes lead to a
warning.