An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7.

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic th Edition
Advertisements

1.
The Web Warrior Guide to Web Design Technologies
Chapter 2 Review Questions
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
Using the Visual Basic Editor Visual Basic for Applications 1.
Chapter 7: Sub and Function Procedures
Chapter 6: More on the Selection Structure
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter 3 Planning Your Solution
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
A First Program Using C#
CIS162AD - C# Decision Statements 04_decisions.ppt.
Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.
Chapter 2: Beginning the Problem-Solving Process
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
Chapter 12: How Long Can This Go On?
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.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 6: The Repetition 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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Chapter 05 (Part III) Control Statements: Part II.
©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.
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
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.
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
An Introduction to Programming with C++ Sixth Edition
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 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
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 Void Functions Tutorial 5.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
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 Beginning the Problem- Solving Process Tutorial 2.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Chapter 8: More on the Repetition Structure
More on the Selection Structure
Value-Returning Functions
The Selection Structure
Chapter 4: Making Decisions.
Microsoft Visual Basic 2005 BASICS
Microsoft Visual Basic 2005: Reloaded Second Edition
WRITING AN ALGORITHM, PSEUDOCODE, AND FLOWCHART LESSON 2.
Presentation transcript:

An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7

An Introduction to Programming with C++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 Include the switch form of the selection structure in pseudocode and in a flowchart Code the switch form of the selection structure in C++

An Introduction to Programming with C++3 Concept Lesson

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

An Introduction to Programming with C++5 Nested Selection Structures

An Introduction to Programming with C++6 Nested Selection Structures

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

An Introduction to Programming with C++8 Logic Errors in Selection Structures

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

An Introduction to Programming with C++10 Results of Desk-checking the Correct Algorithm

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

An Introduction to Programming with C++12 Correct Algorithm and an Incorrect Algorithm Containing the First Logic Error

An Introduction to Programming with C++13 Results of Desk-checking the Incorrect Algorithm

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

An Introduction to Programming with C++15 Correct Algorithm and an Incorrect Algorithm Containing the Second Logic Error

An Introduction to Programming with C++16 Results of Desk-checking the Incorrect Algorithm

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

An Introduction to Programming with C++18 Correct Algorithm and an Inefficient Algorithm Containing the Third Logic Error

An Introduction to Programming with C++19 Results of Desk-checking the Inefficient Algorithm

An Introduction to Programming with C++20 Using the if/else Form to Create Multiple-Path Selection Structures

An Introduction to Programming with C++21 Two Versions of the C++ Code for the Grade Problem

An Introduction to Programming with C++22 Using the switch Form to Create Multiple-Path Selection Structures

An Introduction to Programming with C++23 Using the switch Form Switch statement: –Begins with the switch clause, followed by an opening brace; –Ends with a closing brace Switch clause - composed of the keyword switch followed by a selectorExpression enclosed in parentheses selectorExpression - can contain any combination of variables, constants, functions, methods, and operators, as long as the combination results in a value whose data type is either bool, char, short, int, or long

An Introduction to Programming with C++24 Using the switch Form (Cont.) Each individual clause within 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

An Introduction to Programming with C++25 Results of Desk-checking the Grade Program Example

An Introduction to Programming with C++26 Application Lesson

An Introduction to Programming with C++27 Commission Schedule and Examples

An Introduction to Programming with C++28 IPO Charts

An Introduction to Programming with C++29 Completed Desk-check Tables

An Introduction to Programming with C++30 Coding the main() Function main() function requires two memory locations to store the values of its input and output items Store input value (sales amount) in int variable, sales Store output value (commission amount) in double variable, commission

An Introduction to Programming with C++31 Coding the main() Function

An Introduction to Programming with C++32 Coding the calcCommission() Function calcCommission() function - requires two memory locations: one for the input item and one for the output item Declare and initialize the variable commDollars as double (this will store the output item) Value of input item, sales, is passed to the calcCommission() function when the function is called; the parameter amountSold receives the information

An Introduction to Programming with C++33 Coding the calcCommission() Function

An Introduction to Programming with C++34 Data and Completed Desk-check Table

An Introduction to Programming with C++35 Completing the Commission Program To open the partially completed C++ program: 1.Start Microsoft Visual Studio.NET. If necessary, close the Start Page window 2.Click File on the menu bar, and then click Open Solution. The Open Solution dialog box opens 3.Locate and then open the CppNet\Tut07\T7App Solution folder 4.Click T7App Solution (T7App Solution.sln) in the list of filenames, and then click the Open button 5.If the T7App.cpp source file is not displayed, right-click T7App.cpp in the Solution Explorer window, and then click Open

An Introduction to Programming with C++36 Summary –Use of nested selection structures Pseudocode Flowchart Code –Common logic errors in selection structures –The switch form of the selection structure Pseudocode Flowchart Code