Chapter 6: More on the Selection Structure

Slides:



Advertisements
Similar presentations
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Advertisements

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
The If/Else Statement, Boolean Flags, and Menus Page 180
C++ for Engineers and Scientists Third Edition
CIS162AD - C# Decision Statements 04_decisions.ppt.
Decision Structures and Boolean Logic
Chapter 3 Making Decisions
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
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.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
1 CS161 Introduction to Computer Science Topic #8.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
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.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
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.
An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
More on the Selection Structure
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
The Selection Structure
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
An Introduction to Programming with C++ Fifth Edition
Topics The if Statement The if-else Statement Comparing Strings
Chapter 8: More on the Repetition Structure
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4: Control Structures I (Selection)
Chapter 5: The Selection Structure
WRITING AN ALGORITHM, PSEUDOCODE, AND FLOWCHART LESSON 2.
Presentation transcript:

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

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 Introduction to Programming with C++, Fourth Edition

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++ Introduction to Programming with C++, Fourth Edition

Nested Selection Structures 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 Introduction to Programming with C++, Fourth Edition

Nested Selection Structures (continued) Introduction to Programming with C++, Fourth Edition

Nested Selection Structures (continued) Introduction to Programming with C++, Fourth Edition

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 Introduction to Programming with C++, Fourth Edition

Logic Errors in Selection Structures (continued) Introduction to Programming with C++, Fourth Edition

Logic Errors in Selection Structures (continued) Test Data for Desk-Checking Data for first desk-check Status: F Years: 4 Data for second desk-check Years: 15 Data for third desk-check Status: P Years: 11 Introduction to Programming with C++, Fourth Edition

Results of Desk-Checking the Correct Algorithm Introduction to Programming with C++, Fourth Edition

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 Introduction to Programming with C++, Fourth Edition

Correct Algorithm and an Incorrect Algorithm Containing the First Logic Error Introduction to Programming with C++, Fourth Edition

Results of Desk-Checking the Incorrect Algorithm Introduction to Programming with C++, Fourth Edition

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 Introduction to Programming with C++, Fourth Edition

Correct Algorithm and an Incorrect Algorithm Containing the Second Logic Error Introduction to Programming with C++, Fourth Edition

Results of Desk-Checking the Incorrect Algorithm Introduction to Programming with C++, Fourth Edition

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 Introduction to Programming with C++, Fourth Edition

Correct Algorithm and an Inefficient Algorithm Containing the Third Logic Error Introduction to Programming with C++, Fourth Edition

Results of Desk-Checking the Inefficient Algorithm Introduction to Programming with C++, Fourth Edition

Using the if/else Form to Create Multiple-Path Selection Structures Introduction to Programming with C++, Fourth Edition

Two Versions of the C++ Code for the Grade Problem Introduction to Programming with C++, Fourth Edition

Two Versions of the C++ Code for the Grade Problem (continued) Introduction to Programming with C++, Fourth Edition

Using the switch Form to Create Multiple-Path Selection Structures Introduction to Programming with C++, Fourth Edition

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 Introduction to Programming with C++, Fourth Edition

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 Introduction to Programming with C++, Fourth Edition

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 Introduction to Programming with C++, Fourth Edition

Formatting Numeric Output (continued) Introduction to Programming with C++, Fourth Edition

Formatting Numeric Output (continued) Introduction to Programming with C++, Fourth Edition

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 Introduction to Programming with C++, Fourth Edition

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 Introduction to Programming with C++, Fourth Edition