VISUAL PROGRAMMING
Unit 3 – Program Flow:
Selection Structures
Eng SN Niles
Department of Textile & Clothing Technology
University of Moratuwa
Learning Outcomes
At the end of this lecture you should have an:
understanding of the role of decision structures
in program flow.
appreciation of the different decision structures
in VB
ability to select and use decision structures in an
appropriate manner.
A program will contain a number of
control structures.
Decision structures, which require a
condition to determine whether a sequence
of instructions are to be followed or not.
Loop structures, which require a condition
to determine how often a sequence of
instructions are to be repeated.
Decision Structures
Decision structures are used to tell the
program to follow a sequence of
instructions only if a particular condition
is fulfilled.
The most common decision structure is
the IF structure.
Case 1
If <condition> Then
<action>
End If
Case 2
If <condition> Then
<action1>
Else
<action2>
End If
Case 2
Dim X, Y as Integer
X = 3: Y = 7
If X > Y Then
lblDisplay.Text = “The larger number is “ & X
Else
lblDisplay.Text = “The larger number is “ & Y
End If
Case 3
If condition1 Then
action1
ElseIf condition2 Then
action2
ElseIf condition3 Then
action3
Else action4
End If
Case 3
If Mark > = 85 Then
Grade = “A”
ElseIf Mark >=60 Then
Grade = “B”
ElseIf Mark >=40 Then
Grade = “C”
Else Grade = “F”
End If
Case 4 – Nested Decision
In a certain institution the assessment is as
follows:
A = 85 or more
B = 70 or more
C = 55 or more
D = 40 or more
F = less than 40
Whatever the mark if attendance is less than
80% the grade is F
Case 4 – Nested Decision
If condition-1 Then
If condition-2 Then
action1
Else
action2
End If
Else
action3
End If
If Attendance >=80 Then
If Mark > = 85 Then
Grade = “A”
ElseIf Mark >=60 Then
Grade = “B”
ElseIf Mark >=40 Then
Grade = “C”
Else Grade = “F”
Endif
Else Grade = “F”
End If
Case 5 – Compound IF
If condition-1 AND condition-2 Then
Action1
Else
Action2
End If
E.g.
For a certain module the evaluation is as
follows:
(a) A student earning at least 50% mark will
pass the module
(b) Regardless of the mark earned a student
without a minimum 80% attendance will fail
the module.
If Attendance >=80 AND Mark >=50 Then
Grade = “P”
Else Grade = “F”
End If
Alternately
If Attendance <80 OR Mark <50 Then
Grade = “F”
Else Grade = “P”
End If
AndAlso – if the first condition is false the
second will not be checked
OrElse – the second condition will be checked
only if the first if false
Select-Case
Select Case selector
Case valueList1
action1
Case valueList2
action2
Case Else
action of last resort
End Select
The value list may be of the form:
Case 90, 91, 92
Case Is >=90
Case 90 to 100
IIF Function
If Gender = “M” Then
GenderName = “Male”
Else
GenderName = “Female”
End If
The same can be expressed more succinctly as
follows:
GenderName = IIf(Gender = “M”, “Male”, “Female”)
IF Operator
Wardrobe = If (Gender = “M”, Male(), Female())
Controls to be used with
Selection Structures
Radio Buttons – to select only one of many
options
Checkboxes – to select one or more options
Group boxes (earlier known as frames)