220 likes | 465 Views
Conditional Statements. If Statement. VBA uses Boolean expressions, along with If statements, to control the flow of execution of a program If condition Then ‘here condition is a Boolean expression action1 Else action2 End If. If Statement Flowchart. yes. no. Condition true?.
E N D
If Statement • VBA uses Boolean expressions, along with If statements, to control the flow of execution of a program If condition Then ‘here condition is a Boolean expression action1 Else action2 End If
If Statement Flowchart yes no Condition true? Perform action 1 Perform action 2 Bringing the branches back together gives the flowchart a better structure The arrows are one-way streets. Only one action can (and must) be performed.
If Statement Example IfvarA > varBThen max = varA min = varB Else max = varB min = varA End If • Note: In real life we would use the Max and Min functions but we are keeping things simple to start with
If Statement Example ‘ compute shipping charge, with a discount for more expensive orders If price >= discountShippingPriceThen shippingCharge = price * discountShippingChargeRate Else shippingCharge = price * regularShippingChargeRate End If totalCharge = price + shippingCharge
If Statement Options • The Else part can be omitted: If condition Then action1 End If • There can be multiple ElseIf branches If condition1 Then ‘if condition1 is True do action1, end action1 ElseIf condition2 Then ‘if condition1 is F & condtion2 is T, do action2 action2 Else ‘if both conditions are F, do action3 action3 EndIf
Single Branch If Statement Example ‘ compute shipping charge if applicable shippingCharge = 0 If price < freeShippingPriceThen shippingCharge = price * shippingChargeRate End If price = price + shippingCharge ‘ if the If statement interior is not executed, then shippingCharge is 0
Single Branch If Statement Flowchart yes no Condition true? Perform action Bringing the branches back together gives the flowchart a better structure
Multiple Branch If Statement Example ‘ Set thank you message based on tip size tipPercent = (tipAmount/baseCharge) * 100 IftipPercent < 15 Then txtThankYou.Text = “Thanks.” ElseIftipPercent < 20 Then txtThankYou.Text = “Thank you and have a nice day.” ElseIftipPercent < 25 Then txtThankYou.Text = “Thank you very much! Have a nice day.” Else ‘we know the tip is at least 25% txtThankYou.Text = “Thank you!! Have a GREAT Day!” End If
Multiple Branch If Statement Flowchart yes Condition 1 true? Perform action 1 no yes Perform action 2 Condition 2 true? no yes Condition 3 true? Perform action 3 no The else is optional. The arrows are one- way streets Perform else action
Nesting: If’s inside of If’s • You can nest entire If statements inside the If or Else part of another If statement • Nesting more than one or two deep is strongly discouraged! It makes the program hard to read and understand • Try to use Elseif or more complex conditions instead
Nested If’s Example ‘ Select title based on language and gender If language = “French” Then If gender = “Female” Then title = “Mademoiselle” Else title = “Monsieur” Endif ElseIflanguage = “English” Then If gender =“Female” Then title = “Miss” Else title = “Mister” EndIf Else title = “” ‘no title in this case EndIf
Converting to ElseIfs ‘ Select title based on language and gender If language = “French” Andgender = “Female” Then title = “Mademoiselle” ElseIflanguage = “French” Andgender = “Male” Then title = “Monsieur” ElseIflanguage = “English” Andgender = “Female” Then title = “Miss” ElseIflanguage = “English” Andgender = “Male” Then title = “Mister” Else title = “”‘ it’s usually best to have an else case EndIf
Select Case Statements • The Select Case Statement can be used when there are multiple options to choose from • It can simplify program structure • It makes the logical structure of the program clear when a nested if or long Else If structure might not
Case Statement Example Assume position is a variable with a value between 1 and some number from 2 to 50 SelectCasepostion Case 1 txtOutcome.Text = “Win”‘several lines could go here Case 2 txtOutcome.Text = “Place” Case 3 txtOutcome.Text = “Show” Case 4,5 txtOutcome.Text = “Close but no cigar” Case Else txtOutcome.Text = “Out of the money” End Select
Conditionals Overview We’ve looked at program elements that let us write conditions and branches in programs: • Boolean constants True and False • Comparison operators to form Boolean expressions • Boolean operators to build more complex expressions; truth tables to check them • If statements, three types (with Else, with no Else, with ElseIfs) • Nested If’s • Select Case statements