16
Conditiona l Statements 1

Conditional statements

Embed Size (px)

Citation preview

Page 1: Conditional statements

Conditional Statements

1

Page 2: 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

action1Else

action2End If

2

Page 3: Conditional statements

If Statement Flowchart

Condition true?yes no

Perform action 1 Perform action 2

Bringing the branches back togethergives the flowchart a better structure

3

The arrows are one-way streets.Only one action can (and must)be performed.

Page 4: Conditional statements

If Statement Example

If varA > varB Thenmax = varA

min = varBElse

max = varBmin = varA

End If• Note: In real life we would use the Max and Min

functions but we are keeping things simple to start with

4

Page 5: Conditional statements

If Statement Example‘ compute shipping charge, with a discount for more expensive orders

If price >= discountShippingPrice ThenshippingCharge = price * discountShippingChargeRate

ElseshippingCharge = price * regularShippingChargeRate

End If

totalCharge = price + shippingCharge

5

Page 6: Conditional statements

If Statement Options• The Else part can be omitted:If condition Then

action1End If

• There can be multiple ElseIf branchesIf condition1 Then ‘if condition1 is True do action1, end

action1ElseIf condition2 Then ‘if condition1 is F & condtion2 is T, do action2

action2Else ‘if both conditions are F, do action3

action3EndIf

6

Page 7: Conditional statements

Single Branch If Statement Example ‘ compute shipping charge if applicableshippingCharge = 0If price < freeShippingPrice Then

shippingCharge = price * shippingChargeRateEnd Ifprice = price + shippingCharge

‘ if the If statement interior is not executed, then shippingCharge is 0

7

Page 8: Conditional statements

Single Branch If Statement Flowchart

Condition true?yes no

Perform action

Bringing the branches back togethergives the flowchart a better structure

8

Page 9: Conditional statements

Multiple Branch If Statement Example

‘ Set thank you message based on tip sizetipPercent = (tipAmount/baseCharge) * 100If tipPercent < 15 Then

txtThankYou.Text = “Thanks.”ElseIf tipPercent < 20 Then

txtThankYou.Text = “Thank you and have a nice day.”ElseIf tipPercent < 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

9

Page 10: Conditional statements

Multiple Branch If Statement Flowchart

Condition 1 true?yes

no

Perform action 1

10

no

no

yes

yes

Perform action 2

Perform action 3

Perform else action

Condition 2 true?

Condition 3 true?

The else isoptional. The arrows are one-way streets

Page 11: Conditional statements

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

11

Page 12: Conditional statements

Nested If’s Example‘ Select title based on language and genderIf language = “French” Then

If gender = “Female” Thentitle = “Mademoiselle”Elsetitle = “Monsieur”Endif

ElseIf language = “English” ThenIf gender = “Female” Then title = “Miss”Else title = “Mister”EndIf

Elsetitle = “” ‘no title in this case

EndIf

12

Page 13: Conditional statements

Converting to ElseIfs‘ Select title based on language and genderIf language = “French” And gender = “Female” Then

title = “Mademoiselle”ElseIf language = “French” And gender = “Male” Then

title = “Monsieur”ElseIf language = “English” And gender = “Female” Then

title = “Miss”ElseIf language = “English” And gender = “Male” Then

title = “Mister”Else

title = “” ‘ it’s usually best to have an else caseEndIf

13

Page 14: Conditional statements

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

14

Page 15: Conditional statements

Case Statement ExampleAssume position is a variable with a value between 1 and some number from 2 to 50

Select Case postionCase 1txtOutcome.Text = “Win” ‘several lines could go hereCase 2txtOutcome.Text = “Place”Case 3txtOutcome.Text = “Show”Case 4,5txtOutcome.Text = “Close but no cigar”Case ElsetxtOutcome.Text = “Out of the money”End Select

15

Page 16: Conditional statements

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