Document

advertisement

Programming with Visual C++:

Concepts and Projects

Chapter 4A: Selection

(Concepts)

Objectives

In this chapter, you will:

• 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

Programming with Visual C++: Concepts and Projects 2

Objectives (continued)

• 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

Programming with Visual C++: Concepts and Projects 3

Control Structures

• Control structures

• Are the fundamental building blocks of all programs

• Control the flow of tasks

• Some control structures allow you to build decisionmaking capabilities into programs

• Other control structures enable statement repetition

• Three types of control structures

• Sequential

• Selection

• Repetition

Programming with Visual C++: Concepts and Projects 4

Control Structures (continued)

Programming with Visual C++: Concepts and Projects 5

Sequential Control Structures

• 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

– Example: Figure 4-2 with click event handler code shown in Example 4-1 (following slides)

Programming with Visual C++: Concepts and Projects 6

Sequential Control Structures

(continued)

Programming with Visual C++: Concepts and Projects 7

Sequential Control Structures

(continued)

• Sequential control (3 steps, executed in order)

– Declare score

– Read score

– Display “Pass”

Programming with Visual C++: Concepts and Projects 8

Selection Control Structures

• Selection structures build decision-making capabilities into your program by providing one or more alternative courses of action

– Single alternative selection

– Double alternative selection

– Multiple alternative selection

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

Programming with Visual C++: Concepts and Projects 9

Selection Control Structures

(continued)

Programming with Visual C++: Concepts and Projects 10

Relational Operators

• Boolean expressions compare two values

– For example: score >= 60

• The relationship between two variables is evaluated using the relational operators >, >=,

<, <=, ==, != (see table 4-1 on next slide)

• If more than one relational operator appears in an expression then those with the highest order of precedence are executed first

Programming with Visual C++: Concepts and Projects 11

Relational Operators (continued)

Programming with Visual C++: Concepts and Projects 12

Relational Operators (continued)

• All relational operators are binary (have two operands)

• All expressions involving a relational operator are resolved from left to right (left-to-right associative)

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

Programming with Visual C++: Concepts and Projects 13

Using if Statements to Provide a

Single Alternative

• Syntax:

– keyword if

– Boolean expression

• The Boolean expression must be enclosed in a set of parenthese ( )

• If the Boolean expression evaluates to true then one or more statements are executed

– Example:

• if (score >= 60)txtGrade->Text = “Pass” ;

Programming with Visual C++: Concepts and Projects 14

Using

if

Statements to Provide a

Single Alternative (continued)

• If only one task is to be executed it follows the

Boolean expression

Programming with Visual C++: Concepts and Projects 15

Using if Statements to Provide a

Single Alternative (continued)

• Multiple tasks within an if statement are enclosed in a set of curly brackets { }

Programming with Visual C++: Concepts and Projects 16

Using if Statements to Provide a

Single Alternative (continued)

Programming with Visual C++: Concepts and Projects 17

Using if … else Statements to

Provide Two Alternatives

• The keyword else is used to separate 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

Programming with Visual C++: Concepts and Projects 18

Using if … else Statements to

Provide Two Alternatives (continued)

Programming with Visual C++: Concepts and Projects 19

Using if … else Statements to

Provide Two Alternatives (continued)

Programming with Visual C++: Concepts and Projects 20

Logical Operators

• Logical operators are used to combine two or more relational expressions

• Logical operators

– Not ( !

)

– And ( && )

– Or ( || )

• Pseudocode examples:

– If score is not greater than 60

– If score is greater than 0 and less than 100

– If score is less than 0 or greater than 100

Programming with Visual C++: Concepts and Projects 21

The not Operator (!)

• Reverses a Boolean value

• Has highest precedence among logical operators

• Example: !(score >= 60)

– Assume that score is 45. Then, the relational expression score >= 60 is false

– !

reverses the evaluation to true

Programming with Visual C++: Concepts and Projects 22

The not Operator (!) (continued)

Programming with Visual C++: Concepts and Projects 23

The not Operator (!) (continued)

• The Boolean expression (score >= 60) is

– true (if score is 75)

– false (if score is 25)

• The not operator (!) reverses that evaluation

Programming with Visual C++: Concepts and Projects 24

The and Operator (

&&

)

• 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

Programming with Visual C++: Concepts and Projects 25

The and Operator (&&) (continued)

Programming with Visual C++: Concepts and Projects 26

The and Operator (&&) (continued)

Programming with Visual C++: Concepts and Projects 27

The and Operator (

&&

) (continued)

• 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

Programming with Visual C++: Concepts and Projects 28

The and Operator (

&&

) (continued)

• There are four possible expression evaluation results using the and operator ( && )

Programming with Visual C++: Concepts and Projects 29

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

• Consider a program with:

– Two TextBoxes that must contain integers

– ComboBox control to indicate which arithmetic operation to perform

Programming with Visual C++: Concepts and Projects 30

Determining When to Use the and operator

(

&&

) and the or operator (

||

) (continued)

Programming with Visual C++: Concepts and Projects 31

Determining When to Use the and operator

(

&&

) and the or operator (

||

) (continued)

Programming with Visual C++: Concepts and Projects 32

Determining When to Use the and operator

(

&&

) and the or operator (

||

) (continued)

Programming with Visual C++: Concepts and Projects 33

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

• Possible errors requiring complex expressions to filter out

– No data in txtLeft and txtRight

– Data in one TextBox but not the other

– The TextBoxes have valid data in them but no operation has been selected from the ComboBox

• Example:

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

>Text == “”))

Programming with Visual C++: Concepts and Projects 34

The or Operator (||)

• 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

Programming with Visual C++: Concepts and Projects 35

The or Operator (||) (continued)

Programming with Visual C++: Concepts and Projects 36

The or Operator (||) (continued)

• If either txtLeft or txtRight are empty then display a MessageBox

Programming with Visual C++: Concepts and Projects 37

The or Operator (||) (continued)

Programming with Visual C++: Concepts and Projects 38

Nested Control Structures

• Nested control structures are control structures that are placed inside of one another

• Nesting is used to implement multiple alternative selection

• Common form of selection structure nesting

– Double alternative ( if … else ) statement within one alternative of another if … else statement

Programming with Visual C++: Concepts and Projects 39

Nested Control Structures (continued)

Programming with Visual C++: Concepts and Projects 40

Nested Control Structures (continued)

• The SelectedIndex property of a ComboBox is set to -1 by default or if no selection has been made

Programming with Visual C++: Concepts and Projects 41

Nested Control Structures (continued)

Programming with Visual C++: Concepts and Projects 42

Multiple Alternative Selection

• If there are many alternatives to be selected from then the nesting of if … else statements gets complicated

• Multiple levels of nesting are required

Programming with Visual C++: Concepts and Projects 43

Multiple Alternative Selection

(continued)

Programming with Visual C++: Concepts and Projects 44

Multiple Alternative Selection

(continued)

Programming with Visual C++: Concepts and Projects 45

Multiple Alternative Selection

(continued)

• Multiple alternative selection can be handled in other ways, without nested control structures

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

• Only one alternative (the first one in which the Boolean expression evaluated to true ) is executed

Programming with Visual C++: Concepts and Projects 46

Multiple Alternative Selection

(continued)

Programming with Visual C++: Concepts and Projects 47

Multiple Alternative Selection

(continued)

• The switch statement also implements multiple selection

• Keyword switch is followed by an integral value

• The integral value is used to determine which of several cases (alternatives) will be executed

• Each case has statements associated with it

• Control will transfer out of a case only if a break statement or the end of the switch statement is encountered

Programming with Visual C++: Concepts and Projects 48

Multiple Alternative Selection

(continued)

• Syntax of the switch statement

Programming with Visual C++: Concepts and Projects 49

Multiple Alternative Selection

(continued)

Programming with Visual C++: Concepts and Projects 50

Summary

• 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: Topic of Chapter 5

Programming with Visual C++: Concepts and Projects 51

Summary (continued)

• Types of selection structures

– Single alternative ( if statement)

– Double alternative ( if … else statement)

– Multiple alternative

• Nested if … else statements

• Multiple alternative if ( if … else if statement)

• switch statement

• All if statements evaluate a Boolean expression

Programming with Visual C++: Concepts and Projects 52

Summary (continued)

• Boolean expressions often use relational operators

• >, >=, <, <=, ==, !=

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

Programming with Visual C++: Concepts and Projects 53

Download