Lecture 19 - Flow Control

# Lecture 19 - Flow Control - Flow Control Logical Operators...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Flow Control Logical Operators In addition to using simple conditional operators, more complex conditions can be created using the logical operators And, Or, and Not. The negation operator (Not) is used to change an expression to its opposite state. Not changes an expression's value from true to false or vice-versa. AND and OR Logical operators combine simple logical expressions to create complex logical expressions. Evaluation of a complex logical expression will stop as soon as the computer is able to determine if the entire expression will return true or false. When the And operator is used with two simple expressions, the condition is TRUE only if both expressions are TRUE by themselves. The logical OR operator is applied between two expressions and is satisfied if either (or both) of the expressions is TRUE. Examples Assume that all variables have been declared and have been assigned the following values: sngA = 12.0, sngB = 2.0, intI = 15, intJ = 30, intDone = 0 The expressions would yield the following results: Expression sngA > sngB intI = intJ Or sngA < sngB Or intDone = 1 sngA / sngB > 5 And intI <= 20 intJ > intI And intDone = 0 sngA <> sngB Or intI > intJ Interpretation TRUE ______ ______ ______ ______ Write If statements to check for the following: The value in A1 is between 0 and 100 The value in A1 is less than 10 or greater than 25 Answers If Range("A1").Value >= 0 And Range("A1") <= 100 Then 'Do something End If If Range("A1").Value < 10 Or Range("A1") > 25 Then 'Do something End If If-Then-Else Statements If-Then statements can include an Else statement. If-Then-Else statements are used so that one of the two groups of instructions will be executed, either the first group or the second group. Example If condition Then instruction A1 instruction A2 Else instruction B End If block executed if expression is TRUE block executed if expression is FALSE In this case, if the condition is TRUE instruction A1 and instruction A2 will be executed. If the condition is FALSE, instruction B will be executed. Write If-else statements for the following: If On Hand Less than 50 else If Price If Quantity Equal to 2.99 else Greater than 0 else blnOK = True blnOK = False blnOK = True blnOK = False blnOK = True blnOK = False Answers If intOnHand < 50 Then blnOK = True Else blnOK = False End If If dblPrice = 2.99 Then blnOK = True Else blnOK = False End If If intQuantity > 0 Then blnOK = True Else blnOK = False End If Example of If-Then-Else Calculate Discount on Volume Purchases. Discount is computed at rate1 on the first \$100 and at rate2 on the balance. Dim dblAmount As Double, dblDisc As Double Const conRate1 As Double = 0.05 discount rate 1 Const conRate2 As Double = 0.10 discount rate 2 dblAmount = InputBox("Enter the Amount", "Amount of Purchase") If dblAmount <= 100 Then dblDisc = conRate1 * dblAmount Else dblDisc = conRate1 * 100 + (conRate2 * (dblAmount 100)) End If MsgBox "Discount on " & dblAmount & "is " & dblDisc If-Then-ElseIf statements We can extend this concept by including additional conditions in our if statements. Doing so yields an if-then-elseif chain. General Form If condition1 Then instructions Instructions when condition1 is true ElseIf condition2 Then instructions Instructions when condition2 is true ElseIf condition3 Then instructions Instructions when condition3 is true Else Default Instructions when none of the End If conditions is true If-Then-ElseIf Practice Write an if-then-elseif chain for the following scenario: Income Less than 10000 10000 to 25000 25000 to 50000 More than 50000 5.0% 5.5% 6.0% 6.5% Tax One possible solution... Dim sngIncome As Single, sngTax As Single sngIncome = InputBox("Enter income.") If sngIncome < 10000 Then sngTax = 0.05 ElseIf sngIncome <= 25000 Then sngTax = 0.055 ElseIf sngIncome <= 50000 Then sngTax = 0.06 Else sngTax = 0.065 End If MsgBox "The tax is: " & FormatPercent(sngTax, 1) ...
View Full Document

## This note was uploaded on 04/10/2008 for the course CIS 220 taught by Professor Soper during the Spring '08 term at ASU.

Ask a homework question - tutors are online