CS 455 Lecture Notes

Week Three, Thursday: Control Structures (cont.)

The switch(), break, and continue Keywords

The case labels in the switch() multiple selection structure allow you to write statement blocks without curley braces. You may optionally use them for consistency in style. See page 96 of DD for an example of using switch() to count the letter grades given in a course.

The break statement may also be used to "break out" of a loop. The continue statement is used in a loop to skip the remaining block of code but to continue looping.

The do/while Repitition Structure

This structure always executes its body at least once. Without curley braces the do/while can be confusing. I generally use curley braces for all selection and repitition structures to make the code as easy to read as possible.

Structured Programming Summary

See figure 2.32 (p. 111) of DD for a summary of the control structures.

Rules for Forming Structured Programs

  1. Begin with the simplest flowchart (see Fig. 2.34).
  2. Any rectangle (action) can be replaced with two rectangles in sequence.
  3. Any rectangle can be replaced by any control structure.
  4. Rules 2 and 3 may be applied as often as you like and in any order.

Overlapping blocks are impossible to create if goto is not used.

Thinking about Objects

Elevator technology. Think about the objects in this problem: elevator, door, floor, passenger, buttons. The nouns in the problem description generally correspond to objects (abstract data types). Can you describe in English an algorithm the elevator can follow to do its job?


This page established September 12, 1999; last updated September 12, 1999.