Selection Structures 1 Making Decisions • Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5 2 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. 3 Selection Structure IF THEN Flowchart False/No Condition True/Yes Statements Note: The arms branch right and left Also, notice how the main sequence continues down the middle 4 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. 5 Relational Operators = > >= < <= <> Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to These operators are evaluated from left to right, and are evaluated after any mathematical operators. 6 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. 7 Solving the Overtime Problem 8 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. 9 Nested If Flowchart False/No False/No Condition Condition True/Yes True/Yes False/No Condition True/Yes Notice how the main sequence continues down the middle 10 Nested IF Statements 11 Long Distance Billing Problem 12 Logical Operators • NOT Used to reverse the condition that follows it. • AND Used to combine two relational conditions together. Both conditions must be true in order for the combined condition to be true. • OR Used to combine two relational conditions together. If either of the conditions is true the combined condition is true. • XOR 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 and have the precedence as listed here which can be altered by ( ) 13 NOT Operator Truth Table Condition NOT Condition True False False True NOT 1=1 NOT 5<2 NOT “a”=“b” is FALSE is TRUE is TRUE 14 Second Condition AND Operator Truth Table AND First Condition True False True True False False False False 1=1 AND 2=2 1=1 AND 2=3 “a”=“a” AND 2<4 5<3 AND 6<2 is TRUE is FALSE is TRUE is FALSE 15 Second Condition OR Operator Truth Table OR First Condition True False True True True False True False 1=1 OR 2=2 is TRUE 1=1 OR 2=3 is TRUE “a”=“a” OR 2<4 is TRUE 5<3 OR 6<2 is FALSE 16 Second Condition XOR Operator Truth Table XOR First Condition True False True False True False True False 1=1 XOR 2=2 1=1 XOR 2=3 “a”=“a” XOR 2<4 5<3 XOR 6<2 is FALSE is TRUE is FALSE is FALSE 17 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. 18 Compound Conditions 19 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 20 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. 21 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 22 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 23 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. 24 Nested If Example 1 25 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 26 Select Case Flowchart Select Expression A B DoA DoB … N DoN Each case can contain multiple instructions. The main sequence continues down the middle. 27 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. 28 Select Case Example 1 Select Case Code 1, 3 Tax = .04 2 Tax = .05 else Tax = .02 29 Write a program that inputs a number between 1 and 10 and displays the number with the appropriate twoletter ending (e.g., 1st, 2nd, 3rd, 4th, 5th, ...). 30