Download presentation
Presentation is loading. Please wait.
1
Chapter 5: The Selection Structure
Introduction to Programming with C++ Fourth Edition
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 Introduction to Programming with C++, Fourth Edition
3
Objectives (continued)
Write code that uses comparison operators and logical operators Convert the contents of a char variable to uppercase or lowercase Convert the contents of a string variable to uppercase or lowercase Introduction to Programming with C++, Fourth Edition
4
Using the Selection Structure
Selection structure/decision structure – allows the 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 Must result in either a true or false answer Introduction to Programming with C++, Fourth Edition
5
Selection Structures You Might Use Today
Introduction to Programming with C++, Fourth Edition
6
Including the Selection Structure in Pseudocode
True path - 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 Introduction to Programming with C++, Fourth Edition
7
Including the Selection Structure in Pseudocode (continued)
Introduction to Programming with C++, Fourth Edition
8
Drawing a Flowchart of a Selection Structure
Flowcharts - use standardized symbols to show steps the computer must take to accomplish the program’s goal Diamond symbol (selection/repetition symbol) – used to represent both selection and repetition Introduction to Programming with C++, Fourth Edition
9
Drawing a Flowchart of a Selection Structure (continued)
Introduction to Programming with C++, Fourth Edition
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 not required, it is a good programming practice to use a comment, such as //end if, to mark the end of each if statement Introduction to Programming with C++, Fourth Edition
11
Syntax of the C++ if statement
Introduction to Programming with C++, Fourth Edition
12
Coding the Selection Structure
Introduction to Programming with C++, Fourth Edition
13
Comparison Operators Comparison (relational) operators - to make comparisons in a C++ program Precedence numbers - indicate the order in which the computer performs the comparisons in a C++ expression Parentheses - override the order of precedence Introduction to Programming with C++, Fourth Edition
14
Comparison Operators (continued)
Introduction to Programming with C++, Fourth Edition
15
Comparison Operators (continued)
Introduction to Programming with C++, Fourth Edition
16
Comparison Operators (continued)
Introduction to Programming with C++, Fourth Edition
17
Comparison Operators (continued)
Introduction to Programming with C++, Fourth Edition
18
Swapping Values 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 Introduction to Programming with C++, Fourth Edition
19
Illustration of the Swapping Concept
Introduction to Programming with C++, Fourth Edition
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 Introduction to Programming with C++, Fourth Edition
21
Truth Tables for the And and Or Logical Operators
Introduction to Programming with C++, Fourth Edition
22
Logical Operators Introduction to Programming with C++, Fourth Edition
23
Logical Operators (continued)
Introduction to Programming with C++, Fourth Edition
24
Using Logical Operators in a Program
Data validation - the process of verifying that the input data is within the expected range Introduction to Programming with C++, Fourth Edition
25
Comparing Characters Character comparisons are case-sensitive
‘Y’ is not the same as the character ‘y’ toupper(charVariable) function - converts a character to uppercase tolower(charVariable) function - converts a character to lowercase Introduction to Programming with C++, Fourth Edition
26
Comparing Strings String comparisons are case-sensitive
“YES” is not the same as the string “yes” transform() - converts a string Syntax transform(str.begin(), str.end(), str2.begin, function); For example, if name is a string, the following converts it to lowercase: transform(name.begin(), name.end(), name.begin, tolower); Introduction to Programming with C++, Fourth Edition
27
Summary Selection structure allows the program to make a decision or comparison and then select one of two paths, depending on the result of the comparison Use comparison and logical operators Operations on chars include: Changing case (toupper and tolower) Operations on strings include: transform function Introduction to Programming with C++, Fourth Edition
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.