Chapter 06
Chapter 06
Chapter 06
Chapter 6
More on the Selection Structure
Objectives
1
Making Decisions
2
Making Decisions (cont’d.)
3
Making Decisions (cont’d.)
4
Flowcharting a Nested Selection
Structure (cont’d.)
5
Figure 6-6 Another correct solution to the voter eligibility problem
An Introduction to Programming with C++, Sixth Edition 11
6
Coding a Nested Selection Structure
(cont’d.)
Figure 6-7 Modified problem specification for the health club problem
from Chapter 5’s Lab 5-2
Figure 6-7 Modified program for the health club problem from Chapter 5’s
Lab 5-2
An Introduction to Programming with C++, Sixth Edition 14
7
Figure 6-7 Modified program for the health club problem from
Chapter 5’s Lab 5-2 (cont’d.)
An Introduction to Programming with C++, Sixth Edition 15
8
Logic Errors in Selection Structures
9
Logic Errors in Selection Structures
(cont’d.)
10
First Logic Error
11
First Logic Error (cont’d.)
Figure 6-16 Desk-check table for the incorrect algorithm in Figure 6-15
12
Second Logic Error (cont’d.)
Figure 6-18 Desk-check table for the incorrect algorithm in Figure 6-17
13
Third Logic Error (cont’d.)
Figure 6-20 Desk-check table for the inefficient algorithm in Figure 6-19
14
Multiple-Alternative Selection
Structures
• Sometimes problems require a selection structure
that chooses between several alternatives
• Called multiple-alternative selection structures
or extended selection structures
• In a flowchart, diamond symbol is used; has
multiple flowlines leading out, not just two
• Each flowline represents a possible path, marked
with the value that represents that path
• if/else statements can be used to implement it;
uses multiple if else clauses
An Introduction to Programming with C++, Sixth Edition 29
Multiple-Alternative Selection
Structures (cont’d.)
Figure 6-21 Problem specification for the Kindlon High School problem
15
Multiple-Alternative Selection
Structures (cont’d.)
Figure 6-21 IPO chart for the Kindlon High School problem
Multiple-Alternative Selection
Structures (cont’d.)
16
Figure 6-23 One version of the C++ code for the Kindlon High School problem
An Introduction to Programming with C++, Sixth Edition 33
Figure 6-23 Another version of the C++ code for the Kindlon High School problem
17
The switch Statement
18
The switch Statement (cont’d.)
19
The switch Statement (cont’d.)
20
The switch Statement (cont’d.)
Figure 6-25 IPO chart and C++ instructions for the Warren Company problem
An Introduction to Programming with C++, Sixth Edition 42
21
Summary
Summary (cont’d.)
22
Summary (cont’d.)
• In a switch statement, the data type of the value
in each case clause must be compatible with data
type of selector expression
• Selector expression much evaluate to value of type
bool, char, short, int, or long
• Most case clauses contain a break statement;
tells the computer to leave the switch statement
• Good practice to mark end of switch statement
with a comment (//end switch)
23
Lab 6-2: Plan and Create
24
Figure 6-34 Code for Lab 6-4
An Introduction to Programming with C++, Sixth Edition 49
25