30
1 Selection Structures

1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount

Embed Size (px)

Citation preview

1

Selection Structures

2

Making Decisions

• Sample assignment statements to figure worker pay with possible overtime

PayAmount = Hours * Rate

PayAmount = 40*Rate + (Hours – 40)*Rate*1.5

3

Selection Structure

• Use to make a decision or comparison and then, based on the result of that decision or comparison, to select one of the paths.

• The condition must result in either a true (yes) or false (no) answer.

• If the condition is true, the program performs one set of tasks. If the condition is false, there may or may not be a different set of tasks to perform.

4

Selection Structure IF THEN Flowchart

Condition True/Yes

Statements

False/No

Note: The arms branch right and leftAlso, notice how the main sequence continues down the middle

5

The IF Statement

• The most common decision structure is the IF statement.

• A condition is a Boolean expression that evaluates to either true or false.

• Conditions typically involve one of the six relational operators.

6

Relational Operators

=>>=<<=< >

Equal toGreater thanGreater than or equal toLess thanLess than or equal toNot equal to

These operators are evaluated from left to right, and are evaluated after any mathematical operators.

7

Expressions Containing Relational Operators

10 + 3 < 5 * 2• 5 * 2 is evaluated first,

giving 10• 10 + 3 is evaluated

second, giving 13• 13 < 10 is evaluated

last, giving false

7 > 3 * 4 / 2• 3 * 4 is evaluated first,

giving 12• 12 / 2 is evaluated

second, giving 6• 7 > 6 is evaluated last,

giving true

All expressions containing a relational operator will result in either a true or false answer only.

8

Solving the Overtime Problem

9

Nested Selection Structure

• A nested selection structure is one in which either the true path or the false path includes yet another selection structure.

• Any of the statements within either the true or false path of one selection structure may be another selection structure.

10

Nested If Flowchart

Condition True/YesFalse/No

Condition True/YesFalse/No Condition True/YesFalse/No

Notice how the main sequence continues down the middle

11

Nested IF Statements

12

Long Distance Billing Problem

13

Logical Operators

• Used to reverse the condition that follows it.• Used to combine two relational conditions

together. Both conditions must be true in order for the combined condition to be true.

• Used to combine two relational conditions together. If either of the conditions is true the combined condition is true.

• Used to combine two relational conditions together. If conditions are opposite then combined condition is true.

Note: logical operators are evaluated after any mathematical and relational operators andhave the precedence as listed here which can be altered by ( )

NOTAND

OR

XOR

14

NOT Operator Truth Table

NOT 1=1 is FALSENOT 5<2 is TRUENOT “a”=“b” is TRUE

Condition NOT Condition

True

False

False

True

15

AND Operator Truth Table

1=1 AND 2=2 is TRUE1=1 AND 2=3 is FALSE“a”=“a” AND 2<4 is TRUE5<3 AND 6<2 is FALSE

AND

First Condition

True

False

True

False

Sec

ond

Con

diti

on

True False

False

False

16

OR Operator Truth Table

1=1 OR 2=2 is TRUE1=1 OR 2=3 is TRUE“a”=“a” OR 2<4 is TRUE5<3 OR 6<2 is FALSE

OR

First Condition

True

False

True

True

Sec

ond

Con

diti

on

True False

True

False

17

XOR Operator Truth Table

1=1 XOR 2=2 is FALSE1=1 XOR 2=3 is TRUE“a”=“a” XOR 2<4 is FALSE5<3 XOR 6<2 is FALSE

XOR

First Condition

True

False

False

True

Sec

ond

Con

diti

on

True False

True

False

18

Compound Conditions

• In Visual Logic, a compound condition consists of two conditions within parentheses joined by a logical operator.

• (condition) logical operator (condition)• Requiring the parentheses around the

conditions is syntax requirement of Visual Logic. If you don’t do this you will receive an error or an incorrect result.

19

Compound Conditions

20

Expressions Containing the And Logical Operator

3 > 2 And 6 > 5• 3 > 2 is evaluated

first, giving true• 6 > 5 is evaluated

second, giving true• true And true is

evaluated last, giving true

10 < 25 And 6 > 5 + 1• 5 + 1 is evaluated first, giving 6• 10 < 25 is evaluated second,

giving true• 6 > 6 is evaluated third, giving

false• true And false is evaluated last,

giving false

21

Expression Containing the Or Logical Operator

8 = 4 * 2 Or 7 < 5• 4 * 2 is evaluated first, giving 8• 8 = 8 is evaluated second, giving true• 7 < 5 is evaluated third, giving false• true Or false is evaluated last, giving true

All expressions containing a relational operator will result in either a true or false answer only.

22

Example of Logical Operators used in the condition

• To pass a course, a student must have an average test score of at least 75 and an average project score of at least 35. Write the compound condition using the variables AvgTest and AvgProject.

AvgTest >= 75 And AvgProject >= 35

23

Example of Logical Operators used in the condition

• Only employees with job codes of 34 and 67 will receive a raise. Write the compound condition using the variable Code.

Code = 34 Or Code = 67

24

Nested If Example 1

Show a selection structure that assigns a sales tax rate to the Tax variable. The tax rate is determined by the state code stored in the Code variable. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate.

25

Nested If Example 1

26

Select Case Form of the Selection Structure

• Referred to as the extended selection structure

• Easier than the nested If to write and understand

• Typically used when a selection structure has several paths from which to choose one

27

Select Case Flowchart

Select Expression

DoA DoN

Each case can contain multiple instructions.The main sequence continues down the middle.

DoB

A B N…

28

Select Case Example 1

Write a selection structure that assigns a sales tax rate to the Tax variable. The tax rate is determined by the state code stored in the Code variable. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate.

29

Select Case Example 1

Select CaseCode

Tax = .04 Tax = .05 Tax = .02

1, 3 2 else

30

Write a program that inputs a number between 1 and 10 and displays the number with the appropriate two-

letter ending (e.g., 1st, 2nd, 3rd, 4th, 5th, ...).