chapter 6: more on the selection structure introduction to programming with c++ fourth edition

30
Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Upload: silvester-jordan

Post on 21-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Chapter 6:More on the Selection

Structure

Introduction to Programming with C++

Fourth Edition

Page 2: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 2

Objectives

• Include a nested selection structure in pseudocode and in a flowchart

• Code a nested selection structure in C++• Recognize common logic errors in selection

structures

Page 3: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 3

Objectives (continued)

• Include the switch form of the selection structure in pseudocode and in a flowchart

• Code the switch form of the selection structure in C++

• Format numeric output in C++

Page 4: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 4

Nested Selection Structures

• Nested selection structure: – Selection structure within another selection

structure– Used when more than one decision must be

made before the appropriate action can be taken• Primary decision - always made by the outer

selection structure• Secondary decision - always made by the

inner (nested) selection structure

Page 5: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 5

Nested Selection Structures (continued)

Page 6: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 6

Nested Selection Structures (continued)

Page 7: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 7

Logic Errors in Selection Structures

• Logic errors are commonly made as a result of the following mistakes:– Using a logical operator rather than a nested

selection structure

– Reversing the primary and secondary decisions

– Using an unnecessary nested selection structure

Page 8: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 8

Logic Errors in Selection Structures (continued)

Page 9: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 9

• Test Data for Desk-Checking– Data for first desk-check

• Status: F• Years: 4

– Data for second desk-check• Status: F• Years: 15

– Data for third desk-check• Status: P• Years: 11

Logic Errors in Selection Structures (continued)

Page 10: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 10

Results of Desk-Checking the Correct Algorithm

Page 11: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 11

Using a Logical Operator Rather Than a Nested Selection Structure

• One common error made when writing selection structures is to use a logical operator in the outer selection structure’s condition when a nested selection structure is needed

Page 12: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 12

Correct Algorithm and an Incorrect Algorithm Containing the First Logic

Error

Page 13: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 13

Results of Desk-Checking the Incorrect Algorithm

Page 14: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 14

Reversing the Primary and Secondary Decisions

• Common error: putting the secondary decision in the outer selection structure, and putting the primary decision in the nested selection structure

Page 15: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 15

Correct Algorithm and an Incorrect Algorithm Containing the Second

Logic Error

Page 16: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 16

Results of Desk-Checking the Incorrect Algorithm

Page 17: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 17

Using an Unnecessary Nested Selection Structure

• In most cases, a selection structure containing this error still produces the correct results

• However, it is less efficient than selection structures that are properly structured

Page 18: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 18

Correct Algorithm and an Inefficient Algorithm Containing the Third

Logic Error

Page 19: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 19

Results of Desk-Checking the Inefficient Algorithm

Page 20: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 20

Using the if/else Form to Create Multiple-Path Selection Structures

Page 21: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 21

Two Versions of the C++ Code for the Grade Problem

Page 22: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 22

Two Versions of the C++ Code for the Grade Problem (continued)

Page 23: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 23

Using the switch Form to Create Multiple-Path Selection Structures

Page 24: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 24

Using the switch Form

• Switch statement:– Begins with switch followed by an open brace– Ends with a closing brace

• Switch clause - keyword switch followed by a selectorExpression enclosed in parentheses

• selectorExpression – – Can contain any combination of variables,

constants, functions, methods, and operators– Must result in a bool, char, short, int, or long

Page 25: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 25

Using the switch Form (continued)

• Each clause in the switch statement contains a value followed by a colon

• Data type of the value should be compatible with the data type of the selectorExpression

• Break statement - tells the computer to leave (“break out of”) the switch statement at that point

Page 26: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 26

Formatting Numeric Output

• Use setiosflags stream manipulator to display a program’s numeric output in fixed-point notation only

• Must use #include <iomanip> directive and

using std::ios; and using std::setiosflags;• Use setprecision to control the number of

decimal places displayed

Page 27: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 27

Formatting Numeric Output (continued)

Page 28: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 28

Formatting Numeric Output (continued)

Page 29: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 29

Summary

• Nested selection structure: – Selection structure within another selection structure– Used when more than one decision must be made before

the appropriate action can be taken – Three common logic errors in selection structures

• Logic errors are commonly made as a result of the following mistakes:– Using a logical operator rather than a nested selection

structure

– Reversing the primary and secondary decisions

– Using an unnecessary nested selection structure

Page 30: Chapter 6: More on the Selection Structure Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 30

Summary (continued)

• The switch form of the selection structure is often simpler to use if there are many paths from which to choose

• Format output using stream manipulators