Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Programming with C++1 The Selection Structure Tutorial 6.

Similar presentations


Presentation on theme: "An Introduction to Programming with C++1 The Selection Structure Tutorial 6."— Presentation transcript:

1 An Introduction to Programming with C++1 The Selection Structure Tutorial 6

2 An Introduction to Programming with C++2 Objectives Use the selection structure in a program Write pseudocode for the selection structure Create a flowchart for the selection structure Code the if and if/else forms of the selection structure Write code that uses comparison operators and logical operators Change the contents of a String variable to uppercase or lowercase Compare strings using the CompareTo() method Return a floating-point number when dividing two integers

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

4 An Introduction to Programming with C++4 Using the Selection Structure Selection structure/decision structure – allows program to make a decision or comparison and then select one of two paths, depending on the result of the comparison Condition –Specifies the decision you are making –Phrased so that it results in either a true or false answer

5 An Introduction to Programming with C++5 Selection Structures You Might Use Today

6 An Introduction to Programming with C++6 Including the Selection Structure in Pseudocode True path - the instructions following the condition False path –When using else, false path includes instructions between else and end if –When else is not used, processing continues after the end if

7 An Introduction to Programming with C++7 Including the Selection Structure in Pseudocode

8 An Introduction to Programming with C++8 Drawing a Flowchart of a Selection Structure Flowcharts - use standardized symbols to show steps the computer must take to accomplish program’s goal Diamond symbol (selection/repetition symbol) – used to represent both selection and repetition

9 An Introduction to Programming with C++9 Drawing a Flowchart of a Selection Structure

10 An Introduction to Programming with C++10 Coding the Selection Structure Items in square brackets ([]) in the syntax are optional You create a statement block by enclosing the statements in a set of braces ({}) Although it is not required to do so, it is a good programming practice to use a comment, such as //end if, to mark the end of each if statement

11 An Introduction to Programming with C++11 Syntax of the C++ if statement

12 An Introduction to Programming with C++12 Coding the Selection Structure

13 An Introduction to Programming with C++13 Comparison Operators Comparison (relational) operators - used to make comparisons in a C++ program Precedence numbers - indicate the order in which the computer performs the comparisons in a C++ expression Parentheses - used to override the order of precedence

14 An Introduction to Programming with C++14 Comparison Operators

15 An Introduction to Programming with C++15 Comparison Operators

16 An Introduction to Programming with C++16 Comparison Operators

17 An Introduction to Programming with C++17 Comparison Operators

18 An Introduction to Programming with C++18 Swapping Values Study closely the four instructions that swap the two values: –int temp = 0; creates and initializes a local variable named temp –temp = first; assigns the value contained in the first variable to the temp variable –first = second; assigns the value contained in the second variable to the first variable –second = temp; assigns the value contained in the temp variable to the second variable

19 An Introduction to Programming with C++19 Illustration of the Swapping Concept

20 An Introduction to Programming with C++20 Logical Operators Logical/Boolean operators - And and Or –Allow you to combine two or more conditions into one compound condition And logical operator - all of the conditions must be true for the compound condition to be true Or logical operator - only one of the conditions must be true for the compound condition to be true

21 An Introduction to Programming with C++21 Truth Tables for the And and Or Logical Operators

22 An Introduction to Programming with C++22 Logical Operators

23 An Introduction to Programming with C++23 Logical Operators

24 An Introduction to Programming with C++24 Using Logical Operators in a Program Data validation - the process of verifying that the input data is within the expected range

25 An Introduction to Programming with C++25 Comparing Strings String comparisons are case-sensitive –“Yes” is not the same as the string “YES” or “yes” ToUpper() method - converts a string to uppercase ToLower() method - converts a string to lowercase Member access operator (->) - used to access the members of a class CompareTo() method – used to compare strings

26 An Introduction to Programming with C++26 Comparing Strings

27 An Introduction to Programming with C++27 Using the ToUpper() and CompareTo() Methods in a Program ToUpper() method – converts the contents of the variable to uppercase CompareTo() method - tells the computer to compare the contents of the variable to each of the three state IDs

28 An Introduction to Programming with C++28 Comparing Strings

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

30 An Introduction to Programming with C++30 Analyzing, Planning, and Desk- checking

31 An Introduction to Programming with C++31 Desk-checking Data for first desk-check –Total calories: 150 –Grams of fat: 6 Data for second desk-check –Total calories: 105 –Grams of fat: 2 Data for third desk-check –Total calories: 100 –Grams of fat: -3

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

33 An Introduction to Programming with C++33 Coding the main() Function main() function - requires four memory locations to store the values of its input and output items Use the int data type for variables that store: –total calories, grams of fat, and fat calories (these variables need to store whole numbers only) Use the double data type for the variable that stores: –fat percentage

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

35 An Introduction to Programming with C++35 Coding the calcFatInfo() Function

36 An Introduction to Programming with C++36 Data and Completed Desk-check Table for the Program

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

38 An Introduction to Programming with C++38 Summary –Selection structure As pseudocode In a flowchart As an if and if/else statement –Use comparison operators and logical operators –String operations Uppercase to lowercase Compare strings using the CompareTo() method –Return a floating-point number when dividing two integers


Download ppt "An Introduction to Programming with C++1 The Selection Structure Tutorial 6."

Similar presentations


Ads by Google