45
Computer Programming TCP1224 Chapter 5 The Selection Structure

Computer Programming TCP1224 Chapter 5 The Selection Structure

Embed Size (px)

Citation preview

Page 1: Computer Programming TCP1224 Chapter 5 The Selection Structure

Computer ProgrammingTCP1224

Chapter 5The Selection Structure

Page 2: Computer Programming TCP1224 Chapter 5 The Selection Structure

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

Page 3: Computer Programming TCP1224 Chapter 5 The Selection Structure

•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

Page 4: Computer Programming TCP1224 Chapter 5 The Selection Structure

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

Page 5: Computer Programming TCP1224 Chapter 5 The Selection Structure

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

Page 6: Computer Programming TCP1224 Chapter 5 The Selection Structure

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

Page 7: Computer Programming TCP1224 Chapter 5 The Selection Structure

Using the Selection Structure (continued)

7

Page 8: Computer Programming TCP1224 Chapter 5 The Selection Structure

Pseudocode for Selection Structures

8

Page 9: Computer Programming TCP1224 Chapter 5 The Selection Structure

Flowcharting the if and if/else Selection Structures

9

selection/repetition symbol

Page 10: Computer Programming TCP1224 Chapter 5 The Selection Structure

Coding the if and if/else Selection Structures

10

Page 11: Computer Programming TCP1224 Chapter 5 The Selection Structure

11

Page 12: Computer Programming TCP1224 Chapter 5 The Selection Structure

switch (or case) statements next week. Go back and practice on if/else and flowcharts.

12

Page 13: Computer Programming TCP1224 Chapter 5 The Selection Structure

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

Page 14: Computer Programming TCP1224 Chapter 5 The Selection Structure

Comparison Operators (continued)

14

Page 15: Computer Programming TCP1224 Chapter 5 The Selection Structure

Comparison Operators (continued)

15

Comparison operators are evaluated after any arithmetic operators in the expression

Page 16: Computer Programming TCP1224 Chapter 5 The Selection Structure

Comparison Operator Program 1: Swapping Numerical Values

16

Page 17: Computer Programming TCP1224 Chapter 5 The Selection Structure

17

Page 18: Computer Programming TCP1224 Chapter 5 The Selection Structure

18

!!temp is alocal variable

Page 19: Computer Programming TCP1224 Chapter 5 The Selection Structure

Comparison Operator Program 1: Swapping Numerical Values

19

Page 20: Computer Programming TCP1224 Chapter 5 The Selection Structure

Comparison Operator Program 1: Swapping Numerical Values

20

How the program evaluates and run

Page 21: Computer Programming TCP1224 Chapter 5 The Selection Structure

Comparison Operator Program 2: Displaying the Sum or Difference

21

Page 22: Computer Programming TCP1224 Chapter 5 The Selection Structure

22

Page 23: Computer Programming TCP1224 Chapter 5 The Selection Structure

23

Page 24: Computer Programming TCP1224 Chapter 5 The Selection Structure

Comparison Operator Program 2: Displaying the Sum or Difference

24

Page 25: Computer Programming TCP1224 Chapter 5 The Selection Structure

We have looked at the combination of selection if/else and comparison operators such as != and ==

We now look at logical operators

25

Page 26: Computer Programming TCP1224 Chapter 5 The Selection Structure

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

Page 27: Computer Programming TCP1224 Chapter 5 The Selection Structure

Logical Operators (continued)

27

Page 28: Computer Programming TCP1224 Chapter 5 The Selection Structure

Logical Operators (continued)

28

!! use short-circuit evaluation

Page 29: Computer Programming TCP1224 Chapter 5 The Selection Structure

Using the Truth Tables

•To receive a bonus, a salesperson must be rated A and he/she must sell more than $10,000 in productrating == 'A' && sales > 10000

•To send a letter to all A-rated salespeople and all B-rated salespeoplerating == 'A' || rating == 'B'

29

Page 30: Computer Programming TCP1224 Chapter 5 The Selection Structure

Logical Operators (continued)

30

Page 31: Computer Programming TCP1224 Chapter 5 The Selection Structure

Logical Operators (continued)

31

Page 32: Computer Programming TCP1224 Chapter 5 The Selection Structure

Logical Operator Program: Calculating Gross Pay

32

Page 33: Computer Programming TCP1224 Chapter 5 The Selection Structure

33

data validation

Page 34: Computer Programming TCP1224 Chapter 5 The Selection Structure

Logical Operator Program: Calculating Gross Pay

34

Page 35: Computer Programming TCP1224 Chapter 5 The Selection Structure

Comparing Characters

•Display the word “Pass” if user enters the letter P (uppercase or lowercase)

•Display “Fail” if user enters anything else

35

Page 36: Computer Programming TCP1224 Chapter 5 The Selection Structure

36

Page 37: Computer Programming TCP1224 Chapter 5 The Selection Structure

Converting a Character to Uppercase or Lowercase

37

Page 38: Computer Programming TCP1224 Chapter 5 The Selection Structure

38

Page 39: Computer Programming TCP1224 Chapter 5 The Selection Structure

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

Page 40: Computer Programming TCP1224 Chapter 5 The Selection Structure

40

Converting a String to Uppercase or Lowercase#include <algorithm>

using std::transform;

Page 41: Computer Programming TCP1224 Chapter 5 The Selection Structure

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

Page 42: Computer Programming TCP1224 Chapter 5 The Selection Structure

42

Page 43: Computer Programming TCP1224 Chapter 5 The Selection Structure

transform() Function Program: Calculating Sales Tax (continued)

43

Page 44: Computer Programming TCP1224 Chapter 5 The Selection Structure

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

Page 45: Computer Programming TCP1224 Chapter 5 The Selection Structure

Summary

•Character and string comparisons are case sensitive▫Use toupper(), tolower() and transform()

45