visual c++ programming: concepts and projects chapter 4a selection (concepts)

Post on 21-Dec-2015

224 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS

Chapter 4ASelection (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

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

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

Visual C++ Programming

5

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

Visual C++ Programming

7

Visual C++ Programming

8

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

Visual C++ Programming

10

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

Visual C++ Programming

12

Visual C++ Programming

13

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 { }

Visual C++ Programming

15

Visual C++ Programming

16

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

Visual C++ Programming

18

Visual C++ Programming

19

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.

Logical Operators

Visual C++ Programming

21

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

“not” Logical operators

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

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

Visual C++ Programming

23

Visual C++ Programming

24

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

Visual C++ Programming

26

Visual C++ Programming

27

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

Visual C++ Programming

29

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 == “”))

Visual C++ Programming

31

Visual C++ Programming

32

Visual C++ Programming

33

Visual C++ Programming

34

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

Visual C++ Programming

36

Visual C++ Programming

37

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

Visual C++ Programming

39

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

Visual C++ Programming

41

Visual C++ Programming

42

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

Visual C++ Programming

44

Visual C++ Programming

45

Visual C++ Programming

46

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

Visual C++ Programming

48

Visual C++ Programming

49

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

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

Summary (continued)

Visual C++ Programming

52

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

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

top related