12/5/2020
Information Technology IIA
Lecture 04
By Pramitha Kalansuriya of ATI Galle
Control structures
1
12/5/2020
• Control Statements are used to control the flow of program's
execution. Visual Basic supports control structures such as if... Then,
if...Then ...Else, Select...Case, and Loop structures such as Do
While...Loop, While...Wend, For...Next etc method.
If...Then selection structure
The If...Then selection structure performs an indicated action only
when the condition is True; otherwise the action is skipped.
Syntax of the If...Then selection
If <condition> Then
statement
End If
e.g.: If average>75 Then
txtGrade.Text = "A"
End If
2
12/5/2020
If...Then...Else selection structure
The If...Then...Else selection structure allows the
programmer to specify that a different action is to be
performed when the condition is True than when the
condition is False.
Syntax of the If...Then...Else selection
•
If <condition > Then
statements
Else
statements
End If
e.g.: If average>50 Then
txtGrade.Text = "Pass"
Else
txtGrade.Text = "Fail"
End If
3
12/5/2020
Nested If...Then...Else selection structure
Nested If...Then...Else selection structures test for multiple cases by
placing If...Then...Else selection structures
inside If...Then...Else structures.
Syntax of the Nested If...Then...Else selection
structure
You can use Nested If either of the methods as shown below
• Method 1
If < condition 1 > Then
statements
ElseIf < condition 2 > Then
statements
ElseIf < condition 3 > Then
statements
Else
Statements
End If
4
12/5/2020
Method 2
If < condition 1 > Then
statements
Else
If < condition 2 > Then
statements
Else
If < condition 3 > Then
statements
Else
Statements
End If
End If
End If
Select...Case selection structure
Select...Case structure is an alternative to If...Then...ElseIf for selectively executing a
single block of statements from among multiple block of statements. Select...case is
more convenient to use than the If...Else...End If. The following program block
illustrate the working of Select...Case.
5
12/5/2020
Syntax of the Select...Case selection structure
Select [ Case ] testexpression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select
Example
Dim number As Integer = 8
Select Case number
Case 1 To 5
Msgbox ("Between 1 and 5, inclusive")
Case 6, 7, 8
Msgbox("Between 6 and 8, inclusive")
Case 9 To 10
Msgbox("Equal to 9 or 10")
Case Else
Msgbox("Not between 1 and 10, inclusive")
End Select