214 Control Structures: Part 2 Chapter 5 trol (Web server logs)

214 Control Structures: Part 2 Chapter 5 trol flows to the control variable s increment expression in the header of the forstructure.) Without break, the cases in a switchstatement would run together. Each time a match occurs in the structure, the statements for all the remaining cases will execute. (This feature is perfect for programming the iterative song The Twelve Days of Christmas. ) If no match occurs between the controlling expression s value and a caselabel, the default case executes, and the program draws an error message on the applet. Each casecan have multiple actions. The switchstructure differs from other structures in that it does not require braces around multiple actions in each case. Figure 5.8 shows the general switchstructure flowchart (using a breakin each case). [Note: As an exercise, make a flowchart of the general switchstructure without breaks.] The flowchart makes it clear that each breakstatement at the end of a casecauses control to exit the switch structure immediately. The break statement is not required for the last case in the switch structure (or the default case, when it appears last), because the program continues with the next statement after the switch. Again, note that, besides small circles and arrows, the flowchart contains only rectangle and diamond symbols. It is the programmer s responsibility to fill the rectangles and diamonds with actions and decisions appropriate to the algorithm. Although nested control structures are common, it is rare to find nested switchstructures in a program. casea case a action(s) true false . . . break case b action(s) break false false case z case z action(s) break default action(s) true true case b Fig. 5.8 The switchmultiple-selection structure. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply