1 / 16

Conditional Statements

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?.

nanda
Download Presentation

Conditional Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Conditional Statements

  2. 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

  3. 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.

  4. 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

  5. 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

  6. 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

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

  8. Single Branch If Statement Flowchart yes no Condition true? Perform action Bringing the branches back together gives the flowchart a better structure

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

More Related