CHAP09 - Chapter 9 Additional Control Structures Chapter9

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
153 Chapter 9 Additional Control Structures Chapter 9 ADDITIONAL CONTROL STRUCTURES CHAPTER GOALS To be able to write a Switch statement for a multi-way branching problem. To be able to write a Do-While statement and contrast it with a While statement. To be able to write a For statement as an alternative to a While statement. To understand the purpose of the break and continue statements. To be able to choose the most appropriate looping statement for a given problem. CHAPTER OUTLINE I. The Switch Statement May We Introduce : Admiral Grace Murray Hopper II. The Do-While Statement III. The For Statement IV. The Break and Continue Statements V. Guidelines for Choosing a Looping Statement VI. Problem-Solving Case Study : Monthly Rainfall Averages VII. Testing and Debugging A. Testing and Debugging Hints VIII. Summary GENERAL DISCUSSION This chapter completes the presentation of the C++ control structures. These statements are all optional in that they are not needed in order to write any given program. They are, however, a considerable convenience in the programming process and are thus referred to in the text as the "ice cream and cake of C++." The only new concept is the idea of pretest and posttest loops. The Switch statement is a little more difficult for students than most control structures because of its complicated syntax. Also, there is quite a bit of terminology associated with this single control structure: switch expression, switch label, case label, and default label. If they are presented clearly, Do-While and For are easily understood by most students. Students should be shown how to convert a While loop into a Do-While and vice versa; this is a good way to learn the differences between them. When writing a count-controlled loop with a For statement, a few students overlook the fact that the loop control variable is incremented automatically, and they try to increment it again within the loop body. This mistake does not result in a compile-time error as it would in Pascal. The Pascal compiler imposes two restrictions on the loop control variable of a FOR loop: the variable cannot be modified explicitly within the loop, and the value of the variable is undefined after loop exit. C++ has no such restrictions because a For statement is simply a compact notation for a general While statement. The use of the break statement within loops occasionally can clarify the code, but it is our opinion that students should be taught to use it only as a last resort. Otherwise, it is too easy to lapse into an undisciplined, seat-of-the-pants approach to loop design. Given the variety of looping constructs in C++, be sure to spend some time discussing heuristics for choosing an appropriate loop for a given problem. Chapter 9 is a relatively short chapter and should take less than a week to cover. The first lecture can be
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

CHAP09 - Chapter 9 Additional Control Structures Chapter9

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online