Control Structures
Control Structures
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
picresult
cmdcompare
Clear Compare
cmdclear
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
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
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
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
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
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