Chapter 06

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

An Introduction to

Programming with C++


Sixth Edition

Chapter 6
More on the Selection Structure

Objectives

• Include a nested selection structure in pseudocode


and in a flowchart
• Code a nested selection structure
• Recognize common logic errors in selection
structures
• Include a multiple-alternative selection structure in
pseudocode and in a flowchart
• Code a multiple-alternative selection structure in C++

An Introduction to Programming with C++, Sixth Edition 2

1
Making Decisions

• True and false paths of a selection structure can


contain other selection structures
• Inner selection structures are referred to as nested
selection structures; contained (nested) within an
outer selection structure
• Nested selection structures are used when more
than one decision needs to be made before
choosing an instruction
• Inner (nested) selection structures are indented
within their outer selection structures

An Introduction to Programming with C++, Sixth Edition 3

Figure 6-1 A problem that requires a dual-alternative selection structure


An Introduction to Programming with C++, Sixth Edition 4

2
Making Decisions (cont’d.)

Figure 6-2 A problem that requires a nested single-alternative selection structure

An Introduction to Programming with C++, Sixth Edition 5

Making Decisions (cont’d.)

Figure 6-3 Another problem that requires a dual-alternative selection structure

An Introduction to Programming with C++, Sixth Edition 6

3
Making Decisions (cont’d.)

Figure 6-4 A problem that requires a nested dual-alternative selection structure


An Introduction to Programming with C++, Sixth Edition 7

Flowcharting a Nested Selection


Structure
• Outer and inner selection structures can be thought
of as making primary and secondary decisions,
respectively
• Secondary decision is called such because
whether it needs to be made depends on the result
of a primary decision

An Introduction to Programming with C++, Sixth Edition 8

4
Flowcharting a Nested Selection
Structure (cont’d.)

Figure 6-5 Problem specification for voter eligibility problem

An Introduction to Programming with C++, Sixth Edition 9

Figure 6-5 A correct solution to the voter eligibility problem


An Introduction to Programming with C++, Sixth Edition 10

5
Figure 6-6 Another correct solution to the voter eligibility problem
An Introduction to Programming with C++, Sixth Edition 11

Coding a Nested Selection Structure


• Code for nested selection structures uses the if
and else statements
• Nested selection structures can be placed in either
if or else statement blocks
• Correct tabbing makes code easier to read

An Introduction to Programming with C++, Sixth Edition 12

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

An Introduction to Programming with C++, Sixth Edition 13

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

Coding a Nested Selection Structure


(cont’d.)

Figure 6-8 Sample run of the modified health club program

An Introduction to Programming with C++, Sixth Edition 16

8
Logic Errors in Selection Structures

• Three common logic errors made when writing


selection structures
– Using a compound condition rather than a nested
selection structure
– Reversing the outer and nested decisions
– Using an unnecessary nested selection structure

An Introduction to Programming with C++, Sixth Edition 17

Logic Errors in Selection Structures


(cont’d.)

Figure 6-9 A correct algorithm for the bonus problem

An Introduction to Programming with C++, Sixth Edition 18

9
Logic Errors in Selection Structures
(cont’d.)

Figure 6-10 Test data and manually calculated results

Figure 6-11 Current status of the desk-check table

Figure 6-12 Desk-check table after completing the first desk-check

An Introduction to Programming with C++, Sixth Edition 19

Logic Errors in Selection Structures


(cont’d.)

Figure 6-13 Desk-check table after completing the second desk-check

Figure 6-14 Desk-check table after completing the third desk-check

An Introduction to Programming with C++, Sixth Edition 20

10
First Logic Error

• Using a compound condition rather than a nested


selection structure
• Ignores the hierarchy between two sub-conditions
– One applies only if the other is a certain value

An Introduction to Programming with C++, Sixth Edition 21

First Logic Error (cont’d.)

Figure 6-15 Correct algorithm and an incorrect algorithm


containing the first logic error

An Introduction to Programming with C++, Sixth Edition 22

11
First Logic Error (cont’d.)

Figure 6-16 Desk-check table for the incorrect algorithm in Figure 6-15

An Introduction to Programming with C++, Sixth Edition 23

Second Logic Error


• Reversing the outer and nested selection
structures

Figure 6-17 Correct algorithm and an incorrect algorithm


containing the second logic error
An Introduction to Programming with C++, Sixth Edition 24

12
Second Logic Error (cont’d.)

Figure 6-18 Desk-check table for the incorrect algorithm in Figure 6-17

An Introduction to Programming with C++, Sixth Edition 25

Third Logic Error

• Using an unnecessary nested selection structure


• Often will produce the correct result, but will be
inefficient

An Introduction to Programming with C++, Sixth Edition 26

13
Third Logic Error (cont’d.)

Figure 6-19 Correct algorithm and an incorrect algorithm


containing the third logic error

An Introduction to Programming with C++, Sixth Edition 27

Third Logic Error (cont’d.)

Figure 6-20 Desk-check table for the inefficient algorithm in Figure 6-19

An Introduction to Programming with C++, Sixth Edition 28

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

An Introduction to Programming with C++, Sixth Edition 30

15
Multiple-Alternative Selection
Structures (cont’d.)

Figure 6-21 IPO chart for the Kindlon High School problem

An Introduction to Programming with C++, Sixth Edition 31

Multiple-Alternative Selection
Structures (cont’d.)

Figure 6-22 Flowchart for the Kindlon High School problem


An Introduction to Programming with C++, Sixth Edition 32

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

An Introduction to Programming with C++, Sixth Edition 34

17
The switch Statement

• Can sometimes use the switch statement to code


a multiple-alternative selection structure
• Statement begins with switch keyword followed by
a selector expression in parentheses
• Selector expression can contain any combination of
variables, constants, functions, and operators
• Must result in a data type that is bool, char,
short, int, or long
• Between opening and closing braces (after selector
expression), there are one or more case clauses
An Introduction to Programming with C++, Sixth Edition 35

The switch Statement (cont’d.)

• Each case clause represents a different alternative


and contains a value followed by a colon
• Can include as many case clauses as necessary
• Value for each case clause can be a literal
constant, named constant, or an expression
composed of literal and named constants
• Data type of the value should be the same data
type as the selector expression

An Introduction to Programming with C++, Sixth Edition 36

18
The switch Statement (cont’d.)

• Each case clause contains one or more statements


processed when selector expression matches that
case’s value
• break statement tells computer to break out of
switch at that point; must be the last statement of
a case clause
• Without a break statement, computer continues to
process instructions in later case clauses
• After processing break, computer processes next
instruction after switch statement’s closing brace

An Introduction to Programming with C++, Sixth Edition 37

The switch Statement (cont’d.)

• Good programming practice to document end of


switch with a comment (//end switch)
• Can also include one default clause; processed if
selector expression does not match any values in
case clauses
• default clause can appear anywhere, but usually
entered as last clause
– If it is the last clause, a break statement is not
needed at its end
– Otherwise, a break statement is needed to prevent
computer from processing later case clauses
An Introduction to Programming with C++, Sixth Edition 38

19
The switch Statement (cont’d.)

Figure 6-24 How to use the switch statement


An Introduction to Programming with C++, Sixth Edition 39

Figure 6-24 How to use the switch statement (cont’d.)


An Introduction to Programming with C++, Sixth Edition 40

20
The switch Statement (cont’d.)

Figure 6-25 Problem specification for the Warren Company problem

An Introduction to Programming with C++, Sixth Edition 41

Figure 6-25 IPO chart and C++ instructions for the Warren Company problem
An Introduction to Programming with C++, Sixth Edition 42

21
Summary

• Can nest a selection structure within true or false


path of another selection structure
• Three common logic errors when writing selection
structures
– Using a compound condition instead of a nested
selection structure
– Reversing the inner and outer selection structures
– Using an unnecessary nested selection structure

An Introduction to Programming with C++, Sixth Edition 43

Summary (cont’d.)

• Some solutions require selection structures that


choose from multiple alternatives; called multiple-
alternative or extended selection structures
• Can code these either with if else statements or
the switch statement
• Diamond is used to represent multiple-alternative
selection structures in a flowchart
• Has multiple flowlines leading out; each
representing a possible path and marked with
appropriate values

An Introduction to Programming with C++, Sixth Edition 44

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)

An Introduction to Programming with C++, Sixth Edition 45

Figure 6-26 Flowchart for Lab 6-1


An Introduction to Programming with C++, Sixth Edition 46

23
Lab 6-2: Plan and Create

Figure 6-27 Problem specification for Lab 6-2


An Introduction to Programming with C++, Sixth Edition 47

Lab 6-3: Modify

• Modify the program in Lab 6-2 to calculate


commission based on information in Figure 6-33

Figure 6-33 Problem specification for Lab 6-3


• If the sales are less than zero, display “The sales
cannot be less than zero.”
• If the code is not 1, 2, or 3, display “Invalid Code”

An Introduction to Programming with C++, Sixth Edition 48

24
Figure 6-34 Code for Lab 6-4
An Introduction to Programming with C++, Sixth Edition 49

Lab 6-5: Debug

• Follow the instructions for starting C++ and opening


the Lab6-5.cpp file
• Test the program using codes 1, 2, 3, 4, 5, 9, and -3
• Debug the program

An Introduction to Programming with C++, Sixth Edition 50

25

You might also like