Page 1 of 10
CONTROL STRUCTURES
a. Selection control structures
i. If…..else construct
Used to write programs that require branching depending on a test on some condition
Page 2 of 10
-Syntax is:
If (condition) then
Statement(s)
Else
Statement(s)
End IF
Example
A program 2 accept two numbers and determine the greatest
a) .User interface
b)
Frmcompare
Comparison
Enter first num Txtnum1
Enter second num txtnum2
picresult
cmdcompare
Clear Compare
cmdclear
private sub cmdcompare-click()
dim num1 as integer
dim num2 as integer
num1 = val (txtnum1.text)
num2 = val (txtnum2.text)
if (num1>num2)THEN
picresult.print “num1 is the greatest”
else
picResult.print “num2 is the greatest”
End if
end sub
private sub cmdclear_click()
txtnum1.text = “ “
txtnum2.text=””
End sub.
If with Else If
Used in situations where we have more than one condition to test in the program.
-Its Syntax is:
If (condition1) Then
Page 3 of 10
Statement1
Else if (condition2) Then
Statement2
Else if (condition3)
Statement3
:
.
Else
StatementN
End if
Example
A program to accept student position in class and display category as per the table below
Position Category
1-3 Top 3 student
4-5 Top 5 student
6-10 Top 10 student
a) User interface
Frmcategories
Categorize txtPos
Enter position
picResult
categorize cmdcategorize
private sub cmdcategorize_click()
dim position as integer
position = val(txtpos.text)
if (position >=1) and (position <=3)Then
picresult.print ”top 3 student”
Else if (position > = 4) and (position < = 5)Then
PicResult.print“ top 5 student”
Else if (position > = 6) and ( position<=10) Then
Picresult.print “top 10 student”
Else
Picresult.print ”You are not categorized”
End if
End sub
Page 4 of 10
Exercise
Write a VB program to accept student marks in class and display grade as per the table
below.
Marks Grade
70-100 A
60-69 B
50-59 C
40-49 D
<40 F
a).User interface
Frmgrade
Grade marks
txtmarks
Enter marks
picresult
GRADE cmdgrade
private sub cmdgrade –Click()
Dim marks as integer
Marks = val(txtmarks.text)
If (marks > =70) and (marks <=100) Then
Picresult.print “A”
Else if (marks >=60) and (marks<= 69) Then
Picresult.print”B”
Else if (marks >=50) and (marks<=59) Then
Picresult.print “C”
Else if (marks >= 40 ) and (marks <= 49) Then
Picresult.print “D”
Else if (marks<40) Then
Picresult.print “F”
Else
Picresult.print “Invalid entry”
End if
End sub
Page 5 of 10
ii. CASE CONTRUCT
Programs are more simple to read and understand when a case construct instead of if
construct is used to write programs which involve decisions.
Its Syntax is:
Select Case expression
Case expression1
Statement1
Case expression2
Statement2
⁞
Case else
StatementN
End select
Example
A program to accept the position of a student in a class and display his category as per the
table below.
Position Category
1-3 Top 3 students
4-5 Top 5 students
6-10 Top 10 students
a).User interface
Frmposition
Determination of category txtpos
Key position
picresult
cmdcategory
Categorize
Page 6 of 10
b).Code
Private sub cmdcategory – Click ()
dim position as integer
position = val (txtpos.text)
SELECT CASE position
Case 1 to 3
Picresult.print “top 3 student”
Case 4 to 5
Picresult.print “top 5 student”
Case 6 to 10
Picresult.print “top 10 student”
Case else
Picresult.print ”You entered valid position”
End select
End sub
Exercise
1. Rewrite a program to grade marks using the case construct
2. Write a vb program that accepts the initals of the color and display its name in full as
per table.
Initials Colour name in full
R Red
B Blue
G Green
a).User interface
Frmcategorize
Categorize
txtmarks
Enter marks
picresult
Determine
cmddetermine
b).Code
private sub cmddetermine_click()
dim marks as integer
marks =val(txtmark.text)
SELECT CASE marks
Case 70 to 100
Picresult.print ”A”
Page 7 of 10
Case 60 to 69
Picresult.print “B”
Case 50 to 59
Picresult.print “C”
Case 40 to 49
Picresult.print”D”
Case 0 to 39
Picresult .print “F”
Case else
Picresult.print”You entered invalid marks”
End select
End sub
b).Repetition / iteration control structure
i. The For...Next loop
Used to execute one or more statements for predetermined number of times.
-Syntax is:
For control_variable = initial_value to final_value
Statement(s)
Next control_variable ‘ increment value of control_variable
-You can also make the for loop to skip some iterations e.g
a) For I = 1 to 10 step 2
Picresult.print i
Next i
b) For i= 10 to 1 step -2
Picresult.print i
Next i
Example
A program to display numbers 1 to 5
a).UI
Frmdisplay
Display numbers
picdisplay
cmddisplay
b).Code
Private sub cmddisplay_click()
Dim I as integer
For I = 1 to 5
Picdisplay.print i
Page 8 of 10
Next i
End sub
Example 2
A program to find sum of three numbers entered by the user.
Frmsum
Add numbers
picresult
cmdsum
b).Code
Private sub cmdsum_click()
Dim I as integer
Dim sum as integer
Dim num as integer
Sum = 0
For I = 1 to 3
num = inputbox (“ Key in number”, “ addition”)
sum = sum + num
Next I
Picresult print “sum of numbers =”, sum
End sub
ii) do and while loops
Used to execute one or more statemets based on a test on some condition
a) Do loops
1. Do until (condition) 2. Do while (condition)
Statements(s) Statements(s)
Loop Loop
3. Do 4. Do
Statements (s) Statements(s)
Until (condition) While (condition)
-With loops 1 and 2 the condition is tested before the statements inside the loop are
executed
-With loops 3 and 4 the statements are executed at least once before the condition is
tested.
Page 9 of 10
b).While loop
-It syntax is:
While (condition)
Statement(s)
Wend
-Exactly the same as the Do while..Loop, and as with the Do while..loop, the
While..Wend loop executes statements inside it as long as the condition set is true.
Example
Program using while..wend loop that displays the number series:
3,7,11,15
a).UI
Frmdisplayseries
Display series
picdisplay
cmddisplay
Page 10 of 10
b).Code
Private sub cmddisplay_click()
Dim num as integer
num=3
While (num<=15)
Picdisplay.print num
num=num+4
Wend
End sub