SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
Conditional
Statements
1
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
2
If Statement Flowchart
Condition true?
yes no
Perform action 1 Perform action 2
Bringing the branches back together
gives the flowchart a better structure
3
The arrows are one-way streets.
Only one action can (and must)
be performed.
If Statement Example
If varA > varB Then
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 4
If Statement Example
‘ compute shipping charge, with a discount for more expensive orders
If price >= discountShippingPrice Then
shippingCharge = price * discountShippingChargeRate
Else
shippingCharge = price * regularShippingChargeRate
End If
totalCharge = price + shippingCharge
5
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
6
Single Branch If Statement Example
‘ compute shipping charge if applicable
shippingCharge = 0
If price < freeShippingPrice Then
shippingCharge = price * shippingChargeRate
End If
price = price + shippingCharge
‘ if the If statement interior is not executed, then shippingCharge
is 0
7
Single Branch If Statement Flowchart
Condition true?
yes no
Perform action
Bringing the branches back together
gives the flowchart a better structure
8
Multiple Branch If Statement Example
‘ Set thank you message based on tip size
tipPercent = (tipAmount/baseCharge) * 100
If 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
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 is
optional. The
arrows are one-
way streets
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
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
ElseIf language = “English” Then
If gender = “Female” Then
title = “Miss”
Else
title = “Mister”
EndIf
Else
title = “” ‘no title in this case
EndIf
12
Converting to ElseIfs
‘ Select title based on language and gender
If 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 case
EndIf
13
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
Case Statement Example
Assume position is a variable with a value between 1 and some
number from 2 to 50
Select Case postion
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
15
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 Content

PPT
C presentation book
PPT
Introduction to visual basic programming
PPTX
Vb decision making statements
PPTX
Chapter 6 algorithms and flow charts
PPTX
Decision making statements in C programming
PPSX
Data types, Variables, Expressions & Arithmetic Operators in java
PPTX
Conditional statement in c
PPTX
Control and conditional statements
C presentation book
Introduction to visual basic programming
Vb decision making statements
Chapter 6 algorithms and flow charts
Decision making statements in C programming
Data types, Variables, Expressions & Arithmetic Operators in java
Conditional statement in c
Control and conditional statements

What's hot (20)

PPTX
The Loops
PPTX
Loops in C Programming Language
PPTX
C if else
PPTX
While , For , Do-While Loop
PPTX
Pseudocode
PPTX
Operator.ppt
PPTX
Conditional statement c++
PPTX
If statements in c programming
PPTX
Constants, Variables, and Data Types
PPT
Introduction to C++
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PPTX
Introduction to programming
PPTX
Type Conversion, Precedence and Associativity
PPTX
Conditional Statement in C Language
PDF
Visual Basic 6.0
PPT
Java interfaces
PPTX
Variables in C++, data types in c++
PPTX
Control Statements in Java
The Loops
Loops in C Programming Language
C if else
While , For , Do-While Loop
Pseudocode
Operator.ppt
Conditional statement c++
If statements in c programming
Constants, Variables, and Data Types
Introduction to C++
CONDITIONAL STATEMENT IN C LANGUAGE
Introduction to programming
Type Conversion, Precedence and Associativity
Conditional Statement in C Language
Visual Basic 6.0
Java interfaces
Variables in C++, data types in c++
Control Statements in Java
Ad

Viewers also liked (20)

PDF
Conditional Statements | If-then Statements
PPSX
Conditional statement
PPT
Conditional Statements
PPT
Conditional Statements
PPT
Conditional Statements
PPTX
DATATYPE IN C# CSHARP.net
PPTX
Conditional and biconditional statements
PPT
7.data types in c#
PDF
03 function overloading
PPS
Vb.net session 01
PDF
Octal and Hexadecimal Numbering Systems
PPT
Data types
PPTX
Programming Basics if then else, switch, operators
PDF
Binary octal
PDF
Using the IF Function in Excel
PPTX
11 octal number system
PPT
1st Test - If then, converse, inverse and contrapositive
PPT
C#.NET
PPT
Excel IF function
PPTX
Relationships within the relational database
Conditional Statements | If-then Statements
Conditional statement
Conditional Statements
Conditional Statements
Conditional Statements
DATATYPE IN C# CSHARP.net
Conditional and biconditional statements
7.data types in c#
03 function overloading
Vb.net session 01
Octal and Hexadecimal Numbering Systems
Data types
Programming Basics if then else, switch, operators
Binary octal
Using the IF Function in Excel
11 octal number system
1st Test - If then, converse, inverse and contrapositive
C#.NET
Excel IF function
Relationships within the relational database
Ad

Similar to Conditional statements (20)

PPTX
Decision statements
PPT
Ppt lesson 08
PPTX
If and select statement
KEY
.Net branching and flow control
PPTX
BSc. III Unit iii VB.NET
PPTX
Unit IV Array in VB.Net.pptx
PPT
Vba class 4
PDF
C++ problem solving operators ( conditional operators,logical operators, swit...
PPTX
Computer programming 2 Lesson 9
PPTX
Computer programming 2 - Lesson 7
PPTX
If and select statement
PPTX
Decision structures chpt_5
PPT
Select Case
PPTX
Conditional statements
PDF
Conditional Statements & Loops
PPTX
Decisions
PPTX
Decisions
PPTX
Decisions
PPT
Cprogrammingprogramcontrols
PPTX
Conditionals
Decision statements
Ppt lesson 08
If and select statement
.Net branching and flow control
BSc. III Unit iii VB.NET
Unit IV Array in VB.Net.pptx
Vba class 4
C++ problem solving operators ( conditional operators,logical operators, swit...
Computer programming 2 Lesson 9
Computer programming 2 - Lesson 7
If and select statement
Decision structures chpt_5
Select Case
Conditional statements
Conditional Statements & Loops
Decisions
Decisions
Decisions
Cprogrammingprogramcontrols
Conditionals

More from University of Potsdam (20)

PPTX
Computer fundamentals 01
PPTX
Workshop on android apps development
PDF
Transparency and concurrency
PDF
Database System Architecture
PDF
Functional dependency and normalization
PDF
indexing and hashing
PDF
data recovery-raid
PDF
Query processing
PDF
Machine Learning for Data Mining
PPTX
Tree, function and graph
PDF
Sets in discrete mathematics
PPT
Set in discrete mathematics
PPT
Series parallel ac rlc networks
PPT
Series parallel ac networks
PPT
PDF
PPT
Propositional logic
PDF
Propositional logic
PDF
Prim algorithm
Computer fundamentals 01
Workshop on android apps development
Transparency and concurrency
Database System Architecture
Functional dependency and normalization
indexing and hashing
data recovery-raid
Query processing
Machine Learning for Data Mining
Tree, function and graph
Sets in discrete mathematics
Set in discrete mathematics
Series parallel ac rlc networks
Series parallel ac networks
Propositional logic
Propositional logic
Prim algorithm

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Classroom Observation Tools for Teachers
PDF
RMMM.pdf make it easy to upload and study
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Microbial diseases, their pathogenesis and prophylaxis
2.FourierTransform-ShortQuestionswithAnswers.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Week 4 Term 3 Study Techniques revisited.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Classroom Observation Tools for Teachers
RMMM.pdf make it easy to upload and study
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPH.pptx obstetrics and gynecology in nursing
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra

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 2
  • 3. If Statement Flowchart Condition true? yes no Perform action 1 Perform action 2 Bringing the branches back together gives the flowchart a better structure 3 The arrows are one-way streets. Only one action can (and must) be performed.
  • 4. If Statement Example If varA > varB Then 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 4
  • 5. If Statement Example ‘ compute shipping charge, with a discount for more expensive orders If price >= discountShippingPrice Then shippingCharge = price * discountShippingChargeRate Else shippingCharge = price * regularShippingChargeRate End If totalCharge = price + shippingCharge 5
  • 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 6
  • 7. Single Branch If Statement Example ‘ compute shipping charge if applicable shippingCharge = 0 If price < freeShippingPrice Then shippingCharge = price * shippingChargeRate End If price = price + shippingCharge ‘ if the If statement interior is not executed, then shippingCharge is 0 7
  • 8. Single Branch If Statement Flowchart Condition true? yes no Perform action Bringing the branches back together gives the flowchart a better structure 8
  • 9. Multiple Branch If Statement Example ‘ Set thank you message based on tip size tipPercent = (tipAmount/baseCharge) * 100 If 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
  • 10. 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 is optional. The arrows are one- way streets
  • 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 11
  • 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 ElseIf language = “English” Then If gender = “Female” Then title = “Miss” Else title = “Mister” EndIf Else title = “” ‘no title in this case EndIf 12
  • 13. Converting to ElseIfs ‘ Select title based on language and gender If 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 case EndIf 13
  • 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 14
  • 15. Case Statement Example Assume position is a variable with a value between 1 and some number from 2 to 50 Select Case postion 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 15
  • 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