52
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Embed Size (px)

Citation preview

Page 1: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS

Chapter 4ASelection (Concepts)

Page 2: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Objectives

Visual C++ Programming

2

Discover what control structures are and why they are important

Learn the difference between sequential control structures and selection control structures

Compare values using relational operators Use an if statement to select single

alternative Use an if...else statement to select one of

two alternatives

Page 3: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Objectives (continued)

Visual C++ Programming

3

Develop an understanding of logical operators and their use in complex expressions

Create nested control structures Become familiar with multiple selection

and the switch statement

Page 4: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Control Structures

Visual C++ Programming

4

Control structures Are the fundamental building blocks of all

programs Control the flow of instruction execution

Three types Sequential

Every statement is executed in sequence Selection

Allows you to skip statements Repetition

Allows you to repeat statements

Page 5: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

5

Page 6: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Sequential Control Structures

Visual C++ Programming

6

Linear in nature Each statement is performed in order No statement is skipped No statement is repeated The simplest programs tend to be

sequential in nature

Page 7: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

7

Page 8: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

8

Page 9: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Selection Control Structures

Visual C++ Programming

9

Provide alternative course of action Single alternative selection Double alternative selection Multiple alternative selection

A Boolean expression is evaluated and used to select a course of action

Page 10: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

10

Page 11: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Relational Operators

Visual C++ Programming

11

A Boolean expression often compares two variables

The relationship between two variables is evaluated using relational operators

The equal to (==) and not equal to (!=) operators have lowest precedence

All relational operators are binary (have two operands)

All relational operators are left-to-right associative

Page 12: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

12

Page 13: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

13

Page 14: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Using if Statements to Provide a Single Alternative

Visual C++ Programming

14

Syntax: keyword if followed by a Boolean expression)

If a Boolean expression is true then one or more statements are executed

If only one task is to be executed it can be listed after the Boolean expression

If more than one task is to be executed they must be enclosed in curly brackets { }

Page 15: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

15

Page 16: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

16

Page 17: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Using if…else Statements to Provide Two Alternatives

Visual C++ Programming

17

The keyword else is used to separate the two alternatives

If the Boolean expression is true then the statements in the first alternative are selected

If the Boolean expression is false then the statements in the second alternative are selected

Page 18: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

18

Page 19: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

19

Page 20: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Logical Operators

Visual C++ Programming

20

Operators with Boolean operands Include operators for “and”, “or” and “not” Examples from everyday life. You may have

an exam on Monday but want to go to a concert in another city this weekend. If it is true that I have an exam, then I will not

try to get concert tickets If it is not true that I have an exam, then I will

try to get tickets.

Page 21: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Logical Operators

Visual C++ Programming

21

Operators with Boolean operands Include operators for “and”, “or” and

“not” Logical operators

Not (!) And (&&) Or (||)

Page 22: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

The not Operator (!)

Visual C++ Programming

22

Reverses a Boolean value Has highest precedence among logical

operators Uses the ! Operator Example: !(score >= 60)

Assume that score is 45. Then, the relational expression score >= 60 is false, ! reverses the evaluation to true

Page 23: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

23

Page 24: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

24

Page 25: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

The and Operator (&&)

Visual C++ Programming

25

Used with two Boolean operands – often relational expressions

Lower precedence than not (!) Example:

if ((score > 0) && (score <= 100)) The operands may both be true The left operand may be true and the right

operand false The right operand may be true and the left

operand false

Page 26: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

26

Page 27: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

27

Page 28: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

The and Operator (&&) (continued)

Visual C++ Programming

28

If either the left or right operands are false then the entire expression evaluates to false

The only way and expression evaluates to true using the and operator (&&) is if both operands are true

Page 29: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

29

Page 30: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Determining When to Use the and operator (&&) and the or operator (||)

Visual C++ Programming

30

Consider a program with two TextBoxes that must contain integers and a ComboBox control to indicate which

arithmetic operation to perform Possible errors of omission

No data in both TextBoxes Data in one TextBox but not the other No operation selected from the ComboBox

Example: if ((txtLeft->Text == “”) && (txtRight->Text == “”))

Page 31: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

31

Page 32: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

32

Page 33: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

33

Page 34: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

34

Page 35: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

The or Operator (||)

Visual C++ Programming

35

Unlike the and operator (&&) if either the left or right operands are true then the entire expression evaluates to true

The only way and expression evaluates to false using the or operator (||) is if both operands are false

Page 36: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

36

Page 37: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

37

Page 38: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Nested Control Structures

Visual C++ Programming

38

Nested control structures are ones that are placed inside of another

Often used to implement multiple alternative selection

A double alternative (if…else) statement within one alternative of another if…else statement

Page 39: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

39

Page 40: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Nested Control Structures (continued)

Visual C++ Programming

40

Example: a ComboBox can be evaluated to determine whether a selection has been made.

Valid ComboBox choices have index values starting at 0

If no selection has been made the SelectedIndex property of a ComboBox is set to -1 by default

Page 41: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

41

Page 42: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

42

Page 43: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Multiple Alternative Selection

Visual C++ Programming

43

Can be implemented with nested if…else statements

The statements work like a filter Whichever Boolean expression evaluates

to true first controls that path of execution that the program has

The if…else if statement is made to accommodate multiple selection without nesting

Page 44: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

44

Page 45: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

45

Page 46: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

46

Page 47: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

47

The switch statement also implements multiple selection

Keyword switch is followed by an integral value The integral value is used to determine which

case will be executed Each case has statements that will be executed Control will transfer out only it a break

statement or the end of the switch statement is encountered

Page 48: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

48

Page 49: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Visual C++ Programming

49

Page 50: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Summary

Visual C++ Programming

50

Control structures are the building blocks of every computer program

Types of control structures• Sequential – linear, no statement repeated

or skipped• Selection – allows one or more statements

to be skipped under specific conditions• Repetition – the topic of Chapter 5

Page 51: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Summary (continued)

Visual C++ Programming

51

Types of selection structures• Single alternative (if statement)• Double alternative (if…else statement)• Multiple alternative

Nested if…else statements Multiple alternative if (if…elseif statement) Switch statement

All if statements evaluate a Boolean expression

Page 52: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts)

Summary (continued)

Visual C++ Programming

52

Boolean expressions often use relational operators• >, >=, <, <=, ==, !=

Complex Boolean expressions can be evaluated using logical operators and (&&) and or (||)