Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.

Similar presentations


Presentation on theme: "An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure."— Presentation transcript:

1 An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure

2 An Introduction to Programming with C++, Fifth Edition2 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

3 An Introduction to Programming with C++, Fifth Edition3 Objectives (continued) Convert the contents of a char variable to uppercase or lowercase Convert the contents of a string variable to uppercase or lowercase Use the.NET ToUpper(), ToLower(), and CompareTo() methods Format numeric output in.NET

4 An Introduction to Programming with C++, Fifth Edition4 Concept Lesson Using the Selection Structure Writing Pseudocode for the if and if/else Selection Structures Flowcharting the if and if/else Selection Structures Coding the if and if/else Selection Structures

5 An Introduction to Programming with C++, Fifth Edition5 Concept Lesson (continued) Comparison Operators Logical Operators Comparing Characters Comparing Strings

6 An Introduction to Programming with C++, Fifth Edition6 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 )

7 Using the Selection Structure (continued) An Introduction to Programming with C++, Fifth Edition7

8 8 Writing Pseudocode for the if and if/else Selection Structures

9 An Introduction to Programming with C++, Fifth Edition9 Flowcharting the if and if/else Selection Structures selection/repetition symbol

10 An Introduction to Programming with C++, Fifth Edition10 Coding the if and if/else Selection Structures

11 An Introduction to Programming with C++, Fifth Edition11 Coding the if and if/else Selection Structures (continued)

12 An Introduction to Programming with C++, Fifth Edition12 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 Comparison Operators (continued) An Introduction to Programming with C++, Fifth Edition13

14 An Introduction to Programming with C++, Fifth Edition14 Comparison Operators (continued)

15 An Introduction to Programming with C++, Fifth Edition15 Comparison Operator Program 1: Swapping Numerical Values

16 An Introduction to Programming with C++, Fifth Edition16 Comparison Operator Program 1: Swapping Numerical Values (continued)

17 An Introduction to Programming with C++, Fifth Edition17 temp is a local variable Comparison Operator Program 1: Swapping Numerical Values (continued)

18 An Introduction to Programming with C++, Fifth Edition18 Comparison Operator Program 1: Swapping Numerical Values (continued)

19 An Introduction to Programming with C++, Fifth Edition19

20 An Introduction to Programming with C++, Fifth Edition20 Comparison Operator Program 2: Displaying the Sum or Difference

21 An Introduction to Programming with C++, Fifth Edition21 Comparison Operator Program 2: Displaying the Sum or Difference (continued)

22 An Introduction to Programming with C++, Fifth Edition22 Comparison Operator Program 2: Displaying the Sum or Difference (continued)

23 An Introduction to Programming with C++, Fifth Edition23 Comparison Operator Program 2: Displaying the Sum or Difference (continued)

24 An Introduction to Programming with C++, Fifth Edition24 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

25 Logical Operators (continued) An Introduction to Programming with C++, Fifth Edition25

26 An Introduction to Programming with C++, Fifth Edition26 Logical Operators (continued) use short-circuit evaluation

27 An Introduction to Programming with C++, Fifth Edition27 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'

28 An Introduction to Programming with C++, Fifth Edition28 Logical Operators (continued)

29 An Introduction to Programming with C++, Fifth Edition29 Logical Operators (continued)

30 An Introduction to Programming with C++, Fifth Edition30 Logical Operator Program: Calculating Gross Pay

31 An Introduction to Programming with C++, Fifth Edition31 data validation Logical Operator Program: Calculating Gross Pay (continued)

32 An Introduction to Programming with C++, Fifth Edition32 Logical Operator Program: Calculating Gross Pay (continued)

33 An Introduction to Programming with C++, Fifth Edition33 Comparing Characters Display the word “Pass” if user enters the letter P (uppercase or lowercase) Display “Fail” if user enters anything else

34 An Introduction to Programming with C++, Fifth Edition34 Comparing Characters (continued)

35 An Introduction to Programming with C++, Fifth Edition35 Comparing Characters (continued)

36 An Introduction to Programming with C++, Fifth Edition36 Converting a Character to Uppercase or Lowercase

37 An Introduction to Programming with C++, Fifth Edition37 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()

38 An Introduction to Programming with C++, Fifth Edition38 Converting a String to Uppercase or Lowercase #include using std::transform;

39 An Introduction to Programming with C++, Fifth Edition39 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%

40 An Introduction to Programming with C++, Fifth Edition40 transform() Function Program: Calculating Sales Tax (continued)

41 An Introduction to Programming with C++, Fifth Edition41 transform() Function Program: Calculating Sales Tax (continued)

42 An Introduction to Programming with C++, Fifth Edition42 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 Character and string comparisons are case sensitive –Use toupper(), tolower() and transform()

43 An Introduction to Programming with C++, Fifth Edition43 Application Lesson: Using the Selection Structure in a C++ Program Lab 5.1: Stop and Analyze Lab 5.2 –Create the Willow Springs Health Club program Lab 5.3 –Modify program so that the if statement’s true path handles the invalid data, and its false path handles the valid data Lab 5.4: Desk-Check Lab Lab 5.5: Debugging Lab


Download ppt "An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure."

Similar presentations


Ads by Google