Control Structures in Visual Basic
Control Structures in Visual Basic
Basic
Control Structures
• Control structures allow you to regulate the flow of your program's
execution.
• Using control structures, you can write Visual Basic code that makes
decisions or that repeats actions.
• Other control structures let you guarantee disposal of a resource or
run a series of statements on the same object reference.
• Control Structures can be broadly grouped into
• Decision Structures
• Loop Structures
• Nested Control Structures
Decision Structures
• They are control structures that test a condition then make a decision
based on the results obtained from the tested condition.
• The following illustration shows a decision structure that tests for a
condition being true and takes different actions depending on
whether it is true or false.
Flow chart of Decision Structures
Types of Decision Structures
If...Then selection structure
• The If...Then selection structure performs an indicated action only
when the condition is True; otherwise the action is skipped.
• It only considers one condition
• Syntax of the If...Then selection
• If <condition> Then
statement
End If
Flow chart
.
Start
Condition
Action
No
Exit True
Example
• Think of a program that grades student as pass if they achieve 50
marks and outputs this decision in in a message Box. The G.U.I would
look as follows.
Cont.
• The program would look as follows
Dim marks As Integer = TextBox1.Text
If marks > 49 Then
MessageBox.Show("Pass")
End If
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
Flow Chart
Example 2 (If..Then..Else)
• The same program in example one but now also informing the user
that s/he has failed in case they don’t get above 50.
Dim marks As Integer = TextBox1.Text
If marks > 49 Then
MessageBox.Show("Pass")
Else
MessageBox.Show("Fail")
End If
Cont.
• The following is the G.U.I showing the output.
Multiple If...Then...Else selection structure
• Multiple 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
Method 1/ Approach 1
If < condition 1 > Then
statements
ElseIf < condition 2 > Then
statements
ElseIf < condition 3 > Then
statements
Else
Statements
End If
Cont.
Method 2/ Approach 2
If < condition 1 > Then
statements
Else
If < condition 2 > Then
statements
Else
If < condition 3 > Then
statements
Else
Statements
End If
End If
EndIf
Flow chart
Example to Grade a student based on their
marks using MUT grading Criteria
Dim marks As Integer = TextBox1.Text ElseIf marks > 49 Then
If marks > 100 Then MessageBox.Show("D")
MessageBox.Show("invalid") ElseIf marks < 50 Then
ElseIf marks > 79 Then MessageBox.Show("Fail")
MessageBox.Show("A") Else
ElseIf marks > 69 Then
MessageBox.Show("B") MessageBox.Show("Invalid")
ElseIf marks > 59 Then End If
MessageBox.Show("C")
Cont. Snippets of the output
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.
• Syntax of the Select...Case selection structure
• Select Case Index
Case 0
Statements
Case 1
Statements
End Select
Example: Assume you have to find the grade
using select...case and display in the text box
Dim total_marks as double Case 59 To 50
total_marks= marks.Text MessageBox.Show(“D")
Select Case total_marks Case 49 To 0
Case 100 To 80 MessageBox.Show(“Fail")
MessageBox.Show("A") Case Else
Case 79 To 70 MsgBox "Invalid average
MessageBox.Show(“B") marks"
Case 69 To 60 End Select
MessageBox.Show(“C")
Task
• Consider a real life learning scenario in MUT where students sit for
CATs, Assignments, and final examination. Create a windows form
application that allows the user to enter the marks for CAT1, CAT2,
Assignment, and Final Exam marks. The program then calculates the
total marks and grades the student using the MUT grading criteria.