Download presentation
Presentation is loading. Please wait.
Published byMark Parrish Modified over 9 years ago
1
Computer Programming TCP1224 Chapter 5 The Selection Structure
2
Comments Note that quizzes, mid-term and exam questions may not come exactly from lecture slides. ▫The text book helps! (only about 15-20 pages per chapter which you can read and understand in about 30-60 minutes per week). ▫You need to do some of your own programming in order to prepare yourself better. 2
3
3 important aspects of programming ▫Sequence (we have experience it) ▫Selection (is that we are going to talk about this next 2 weeks) ▫Repetition 3
4
Objectives 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 4
5
Objectives (continued) Convert the contents of a char variable to uppercase or lowercase Convert the contents of a string variable to uppercase or lowercase 5
6
Using the Selection Structure Also called the decision structure ▫Condition specifies decision Results in either a true or false answer only ▫Three forms: if, if/else, and switch (or case ) 6
7
Using the Selection Structure (continued) 7
8
Pseudocode for Selection Structures 8
9
Flowcharting the if and if/else Selection Structures 9 selection/repetition symbol
10
Coding the if and if/else Selection Structures 10
11
11
12
switch (or case ) statements next week. Go back and practice on if/else and flowcharts. 12
13
Comparison Operators Often called relational operators If expression has multiple comparison operators at same precedence, it is evaluated from left to right Comparison operators are evaluated after any arithmetic operators in the expression 13
14
Comparison Operators (continued) 14
15
Comparison Operators (continued) 15 Comparison operators are evaluated after any arithmetic operators in the expression
16
Comparison Operator Program 1: Swapping Numerical Values 16
17
17
18
18 !! temp is a local variable
19
Comparison Operator Program 1: Swapping Numerical Values 19
20
Comparison Operator Program 1: Swapping Numerical Values 20 How the program evaluates and run
21
Comparison Operator Program 2: Displaying the Sum or Difference 21
22
22
23
23
24
Comparison Operator Program 2: Displaying the Sum or Difference 24
25
We have looked at the combination of selection if/else and comparison operators such as != and == We now look at logical operators 25
26
Logical Operators Logical operators allow you to combine two or more conditions into one compound condition ▫Sometimes called Boolean operators And/Or operators are evaluated after any arithmetic or comparison operators in an expression 26
27
Logical Operators (continued) 27
28
Logical Operators (continued) 28 !! use short-circuit evaluation
29
Using the Truth Tables To receive a bonus, a salesperson must be rated A and he/she must sell more than $10,000 in product rating == 'A' && sales > 10000 To send a letter to all A-rated salespeople and all B-rated salespeople rating == 'A' || rating == 'B' 29
30
Logical Operators (continued) 30
31
Logical Operators (continued) 31
32
Logical Operator Program: Calculating Gross Pay 32
33
33 data validation
34
Logical Operator Program: Calculating Gross Pay 34
35
Comparing Characters Display the word “Pass” if user enters the letter P (uppercase or lowercase) Display “Fail” if user enters anything else 35
36
36
37
Converting a Character to Uppercase or Lowercase 37
38
38
39
Comparing Strings String comparisons are case-sensitive ▫“yes”, “YES”, and “Yes” are different Before using a string in a comparison, convert it to uppercase or lowercase ▫Use transform() 39
40
40 Converting a String to Uppercase or Lowercase #include using std::transform;
41
transform() Function Program: Calculating Sales Tax Calculate and display the amount of sales tax that a customer owes Tax rate is 3% of purchase price If purchase is made in Gunter County, tax rate is 3.25% 41
42
42
43
transform() Function Program: Calculating Sales Tax (continued) 43
44
Summary The selection structure (decision structure) is one of the three programming structures ▫Forms: if, if/else, and switch (or case ) ▫Represented by a diamond in a flowchart ▫Use the C++ if statement to code both the if and if/else forms Condition can contain variables, constants, functions, and arithmetic, comparison, or logical operators 44
45
Summary Character and string comparisons are case sensitive ▫Use toupper(), tolower() and transform() 45
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.