This preview shows page 1. Sign up to view the full content.
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 viceversa. 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 IfThenElse Statements
IfThen statements can include an Else statement. IfThenElse 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 Ifelse 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 IfThenElse
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 IfThenElseIf statements
We can extend this concept by including additional conditions in our if statements. Doing so yields an ifthenelseif 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 IfThenElseIf Practice
Write an ifthenelseif 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
 Spring '08
 Soper

Click to edit the document details