Visual Basic Control Structures – Constructs
They define the order in which instructions a program will be executed.
Types
1. Sequence Constructs
2. Selection Constructs
3. Looping/ Repetition/ Iteration constructs
1. Sequence Constructs
Instructions are executed in the order in which they appear from the first line to the last . All instructions are executed
2. Selection Constructs
Brings reasoning and decision making in programming
Instructions are executed if a condition is met. Not all instructions are executed.
Examples of selection constructs
a. If…Then construct
b. If … Then …Else construct
c. If…ElseIf… Else construct
d. Select Case construct
a. If construct
it tests a condition and execute some code if the condition is met
Syntax
If (condition) Then
code to execute if the condition is met.
End If
Example
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnCheck.Click
REM This is a comment
' this is another comment
' This program checks if a number is divisible by another number
' Declare the variables
Dim num1 As Integer
Dim num2 As Integer
Dim remainder As Integer
REM Get input from the user
num1 = txtNumber1.Text
num2 = txtNumber2.Text
' Find the remainder
remainder = num1 Mod num2
REM This is a comment
' this is another comment
REM Check if a number is even or odd
If remainder = 0 Then
lblDisplay.Text = "First number is divisble by the second. The remainder is " & remainder
End If
End Sub
b. If…Then… Else construct
it tests a condition and execute some if the condition is met. It also provides an alternative set of code to execute if the condition is
not met.
Syntax
If (condition) Then
code to execute if the condition is met.
Else
Alternative code to execute if the condition is NOT met.
End If
Example
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnCheck.Click
REM This is a comment
' this is another comment
' This program checks if a number is divisible by another number
' Declare the variables
Dim num1 As Integer
Dim num2 As Integer
Dim remainder As Integer
REM Get input from the user
num1 = txtNumber1.Text
num2 = txtNumber2.Text
' Find the remainder
remainder = num1 Mod num2
REM This is a comment
' this is another comment
REM Check if a number is even or odd
If remainder = 0 Then
lblDisplay.Text = "First number is divisble by the second. The remainder is " & remainder
Else
lblDisplay.Text = "First number is NOT divisble by the second. The remainder is " & remainder
End If
End Sub
c. If…ElseIf…Else construct
It evaluates multiple conditions and for each condition that is met, some codes are executed. It also provides an alternative set of
code to execute if ALL the conditions are not met.
Syntax
If (condition1) Then
code to execute if condition1 is met
ElseIf (condition2) Then
code to execute if condition2 is met
ElseIf (condition3) Then
code to execute if condition3 is met
Else
code to execute if ALL the conditions are not met
End If
Example
Public Class Form1
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnClose.Click
End
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnCalculate.Click
' Criteria 75-100 A, 60-74 B,
'50-69 C, 40-49 FAI
' any other mark is INVALID
Dim Maths As Single
Dim Kis As Single
Dim Social As Single
Dim English As Single
Dim Total As Single
Dim Average As Single
Dim Grade As String
Maths = Val(txtMaths.Text)
Kis = Val(txtKiswahili.Text)
Social = Val(txtSocial.Text)
English = Val(txtEnglish.Text)
Total = Social + Maths + English + Kis
Average = Total / 4
If ((Average >= 75) And (Average <= 100)) Then
Grade = "A"
ElseIf ((Average >= 60) And (Average < 75)) Then
Grade = "B"
ElseIf ((Average >= 50) And (Average < 60)) Then
Grade = "C"
ElseIf ((Average >= 40) And (Average < 50)) Then
Grade = "D"
ElseIf ((Average >= 0) And (Average < 40)) Then
Grade = "FAIL"
Else
Grade = "Invalid Data Entry. Try again"
End If
lblDisplay.Text = Grade
lblAverage.Text = Average
lblTotal.Text = Total
End Sub
End Class
d. Select Case construct
Assignment
i. Describe the Visual Basic Select case construct
ii. Write down the VB Select Case syntax
iii. Rewrite the grading program and use Select Case instead of If ElseIf construct
3. Looping Constructs
A loop is used to performs a task repeatedly as long as a condition is met.
A loop executes some code repeatedly as long as a condition is met.
4. Looping Constructs
A loop is used to performs a task repeatedly as long as a condition is met.
A loop executes some code repeatedly as long as a condition is met.
Types of Loops
a. Do…while loop
b. While loop
c. For loop
a. Do…While loop
It is a post condition loop because it checks the condition after executing its code at least once. It executes its code at least once even if the
condition is false.
Syntax
Do
code to execute
increment / Decrement operation to control the loop in relation to the condition
Loop While condition
Example
The following program generates a list of 9 numbers in the order 3,5,7 and then find their sum.
Public Class Form1
Private Sub BtnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
Dim count As Integer ' to count 1-9
Dim seriesValue As Integer ' to store the series values 3,5,7 etc
Dim sum As Integer ' to store the sum of the series values
count = 1
seriesValue = 3
sum = 0
Do
lstSeries.Items.Add(seriesValue)
sum = sum + seriesValue
seriesValue = seriesValue + 2
count = count + 1 ' increment operation
Loop While count <= 9
lstSeries.Items.Add("The sum of all is = " & sum)
End Sub
Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
End
End Sub
End Class
b. While loop
It is a precondition loop because it evaluates the condition before executing its code. This means that if the condition is false, its code
will never be executed.
It will execute its code as long as the condition is true.
Syntax
While condition
code to execute
increment or decrement operation
End While
Example
The following program generates a list of 12 numbers in the order 3,12,48 and then find their sum.
Public Class Form1
Private Sub BtnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
Dim count As Integer ' to count 1-12
Dim seriesValue As Integer ' to store the series values 3,12,48 etc
Dim sum As Integer ' to store the sum of the series values
count = 1
seriesValue = 3
sum = 0
While count <= 12
lstSeries.Items.Add(seriesValue)
sum = sum + seriesValue
seriesValue = seriesValue * 4
count = count + 1 ' increment operation
End While
lstSeries.Items.Add("The sum of all is = " & sum)
End Sub
Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
End
End Sub
End Class
c. For loop
The for loop executes its code for a specific number of times as specified within the condition.
Syntax
For StartValue To StopValue
code to execute
Next value
Example
The following program generates a series of 7 numbers in the order 95,90,85 and find their sum.
Public Class Form1
Private Sub BtnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
Dim count As Integer ' to count 1-7
Dim seriesValue As Integer ' to store the series values 95,90,85 etc
Dim sum As Integer ' to store the sum of the series values
seriesValue = 95
sum = 0
For count = 1 To 7
lstSeries.Items.Add(seriesValue)
cboSeries.Items.Add(seriesValue)
sum = sum + seriesValue
seriesValue = seriesValue - 5
Next count
lstSeries.Items.Add("The sum of all is = " & sum)
cboSeries.Items.Add("The sum of all is = " & sum)
End Sub
Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
End
End Sub
End Class