Control Structures I II
Control Structures I II
Selection / Decisions
SELASIE BROWN
Control Structures
◼Sequential
◼Selection / Decisions
◼Repetition / Looping
VB Decisions
◼Visual Basic decision statements
▪ If…Then
▪ one-way selection structure
▪ If…Then…Else
▪ two-way selection structure
▪ If…Then…ElseIf
▪ multi-way selection structure
▪ If…Then…ElseIf…Else
▪ multi-way selection structure
▪ Select Case
▪ multi-way selection structure
If…Then Decision Structure
◼If…Then decision structure
provides one choice
◼Evaluate the condition: True
Conditio
True or False n
Ex: Is it cold outside?
False
◼True – execute code
Ex: If yes, wear a coat Conditional
Code
◼False – do not execute code
Ex: If no,
If…Then Statement Syntax
If condition Then
statement[s]
End If
◼Syntax explanation:
▪ If , Then, and End – Keywords
▪ Condition – True/False value, variable, function call
or expression
▪ Statement[s] – one or more code statements to be
executed if condition is true
Conditions
◼Evaluate condition: n
True or False
◼True – execute code Statement(s)
If False
Statement(s)
If True
in If…Then block
◼False – execute code
in Else Block
◼One of the two choices must be selected
◼They are mutually exclusive
If…Then…Else Syntax
If condition Then
statement[s]1
Else
statement[s]2
End If
◼Syntax explanation:
▪ If , Then, Else, and End – Keywords
▪ Condition – True/False value, variable, function call or
expression
▪ Statement[s]1 – executed if condition is True
▪ Statement[s]2 – executed if condition is False
If…Then…Else Examples
If (intSales > 50000) Then
blnGetsDoubleBonus = True
decBonus = 4000.00
Else
decBonus = 2000.00
End If
-----------------------------------------------------------------
If (Not(isNumeric(txtInput.text))) Then
MsgBox(“You did not enter a valid number – program will end”)
End
Else
intNumber = Val(txtInput.text)
End If
-----------------------------------------------------------------
If (intTemp >= 60)And(intTemp < 90)And(VisibRating() > 5) Then
lblMessage.text = “Go - Weather conditions are ideal”
Else
lblMessage.text = “Wait - Weather conditions unacceptable”
End If
If…Then…ElseIf Decision Structure
◼If…Then…ElseIf True
allows for multiple C
1 Statement(s)1
False
mutually exclusive True
choices C
2
Statement(s)2
If…Then…ElseIf False