Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7."— Presentation transcript:

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

2 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++

3 An Introduction to Programming with C++3 Concept Lesson

4 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

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

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

7 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

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

9 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

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

11 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

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

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

14 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

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

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

17 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

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

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

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

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

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

23 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

24 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

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

26 An Introduction to Programming with C++26 Application Lesson

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

28 An Introduction to Programming with C++28 IPO Charts

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

30 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

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

32 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

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

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

35 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

36 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


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

Similar presentations


Ads by Google