An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic th Edition
1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Introduction to Flowcharting
Selection (decision) control structure Learning objective
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions.
An Introduction to Programming with C++ Fifth Edition
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.
Chapter 6: More on the Selection Structure
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
Chapter 4 Selection Structures: Making Decisions.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
1 Program Planning and Design Important stages before actual program is written.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
An Introduction to Programming with C++ Sixth Edition
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
The if…else Selection Statement
More on the Selection Structure
An Introduction to Programming with C++ Sixth Edition
Chapter 4: Making Decisions.
Chapter 4 MATLAB Programming
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 4: Making Decisions.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
Chapter 8: More on the Repetition Structure
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 5: The Selection Structure
Presentation transcript:

An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure

An Introduction to Programming with C++, Fifth Edition2 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

An Introduction to Programming with C++, Fifth Edition3 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++ Display a message along with the contents of one or more variables in a.NET program

An Introduction to Programming with C++, Fifth Edition4 Concept Lesson Nested Selection Structures Logic Errors in Selection Structures Using the if/else Form to Create Multiple-Path Selection Structures Using the switch Form to Create Multiple-Path Selection Structures

An Introduction to Programming with C++, Fifth Edition5 Nested Selection Structures A nested selection structure is contained within the outer selection structure –In the true path or in the false path –Use if more than one decision must be made before appropriate action can be taken Primary decision Secondary decision

An Introduction to Programming with C++, Fifth Edition6 Nested Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition7 Nested Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition8 Nested Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition9 Nested Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition10 Another Example of a Nested Selection Structure Lab 5.2: program for Marshall Cabello –Allows Marshall to enter number of calories and grams of fat contained in a specific food –Calculates and displays: Food’s fat calories Its fat percentage Modify program so that it displays “This food is high in fat.” if fat percentage is > 30% –Otherwise, display “This food is not high in fat.”

An Introduction to Programming with C++, Fifth Edition11 Another Example of a Nested Selection Structure (continued)

An Introduction to Programming with C++, Fifth Edition12 Another Example of a Nested Selection Structure (continued)

An Introduction to Programming with C++, Fifth Edition13 Logic Errors in Selection Structures Logic errors in selection structures are usually the result of: –Using a logical operator rather than a nested selection structure –Reversing the primary and secondary decisions –Using an unnecessary nested selection structure XYZ Company’s bonus program will be used to demonstrate these errors

An Introduction to Programming with C++, Fifth Edition14 Logic Errors in Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition15 Logic Errors in Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition16 Using a Logical Operator Rather than a Nested Selection Structure

Using a Logical Operator Rather than a Nested Selection Structure (continued) An Introduction to Programming with C++, Fifth Edition17

An Introduction to Programming with C++, Fifth Edition18 Reversing the Primary and Secondary Decisions

Reversing the Primary and Secondary Decisions (continued) An Introduction to Programming with C++, Fifth Edition19

An Introduction to Programming with C++, Fifth Edition20 Using an Unnecessary Nested Selection Structure

Using an Unnecessary Nested Selection Structure (continued) An Introduction to Programming with C++, Fifth Edition21

An Introduction to Programming with C++, Fifth Edition22 Using the if/else Form to Create Multiple-Path Selection Structures You can create a selection structure that can choose from several alternatives –Multiple-path selection structure Also called extended selection structure –E.g., display message based on letter grade

An Introduction to Programming with C++, Fifth Edition23 Using the if/else Form to Create Multiple-Path Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition24 Using the if/else Form to Create Multiple-Path Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition25 Using the switch Form to Create Multiple-Path Selection Structures If selection structure has many paths from which to choose, it is often simpler to use the switch form –Also known as the case form

An Introduction to Programming with C++, Fifth Edition26 Using the switch Form to Create Multiple-Path Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition27 Using the switch Form to Create Multiple-Path Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition28 Using the switch Form to Create Multiple-Path Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition29 Desk-Checking the Grade Program Input ‘b’ –cout << "Above Average" << endl; displays the message on the screen –break; tells computer to skip remaining instructions in switch Input ‘D’ –cout << "Below Average" << endl; –break; Input ‘X’ –default clause displays "Input error"

An Introduction to Programming with C++, Fifth Edition30 Switch Statement Program: Displaying a Price A program needs to display a price based on a product ID entered by the user The valid product IDs and their prices are shown in the following chart

An Introduction to Programming with C++, Fifth Edition31 Switch Statement Program: Displaying a Price (continued)

An Introduction to Programming with C++, Fifth Edition32 Summary You can nest a selection structure within either the true path or false path of another selection structure Common errors when writing selection structures –Using a logical operator when a nested selection structure is needed –Reversing the primary and secondary decisions –Using an unnecessary nested selection structure Code multiple-path selection structures using if/else or switch –The flowchart symbol for the switch is a diamond

An Introduction to Programming with C++, Fifth Edition33 Application Lesson: Using the Selection Structure in a C++ Program Lab 6.1: Stop and Analyze Lab 6.2 –Jennifer Yardley owns Golf Pro –Salespeople receive commission based on their total domestic and foreign sales Lab 6.3 –Modify program from Lab 6.1 by replacing if statements with a switch Lab 6.4: Desk-Check Lab Lab 6.5: Debugging Lab