Top Banner
1 Selection Structures
30

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

Dec 22, 2015

Download

Documents

Milton McKinney
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.

1

Selection Structures

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

2

Making Decisions

• Sample assignment statements to figure worker pay with possible overtime

PayAmount = Hours * Rate

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

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

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.

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

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

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

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.

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

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.

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

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.

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

8

Solving the Overtime Problem

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

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.

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

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

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

11

Nested IF Statements

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

12

Long Distance Billing Problem

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

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

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

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

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

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

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

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

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

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

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

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.

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

19

Compound Conditions

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

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

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

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.

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

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

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

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

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

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.

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

25

Nested If Example 1

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

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

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

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…

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

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.

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

29

Select Case Example 1

Select CaseCode

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

1, 3 2 else

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

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, ...).