VB Control Structures
VB Control Structures
VB Control Structures
VB
CONTROL STRUCTURES
Control Statements are used to control the
flow of program's execution.
Using control structures, you can write
Visual Basic code that makes decisions or
that repeats actions.
Private Sub Form_Load()
Form1.Show {first to execute}
Print "Hello” {second to execute}
End Sub
DECISION/SELECTION
if......then
2) if ..... Then ...else
3) If ..... then ..... ElseIf
4) Case
IF……..THEN
The If...Then selection structure performs an
indicated action only when the condition is True;
otherwise the action is skipped.
syntax:
If {condition} then
{statements}
End if
Example program to determine
student age
Pseudo code/Algorithms
Age=value(txtage.text)
If txtage.text>=18 then
End if
End sub
EXAMPLE IN A PROGRAM
Private Sub cmdcheck_Click()
If txtmarks.text>100 Then
msgbox(“Invalid Input”)
End if
End sub
IF….THEN….ELSE
When a program must select from more
than one alternative we use
If <condition > Then
vb statements
Else
vb statements
End If
Flow chart
EXAMPLE
Private Sub cmdcheck_Click()
If txtage.text >18 then
msgbox(“adult”)
Else
Lblresult.caption=“minor”
End if
End sub
ASSIGNMENT
WRITE a program that will check if a
number
entered on txt grade is greater than 50 if it
is it
should print {pass} if not {fail} on a LABEL
your code must be in command button
named check
70-100 A
60-69 B
50-59 C
40-49 D
<40 F
GRADING PROGRAM
Dim m As Integer
m = InputBox("Enter student marks", "marks")
If m >= 0 And m < 41 Then
lblresults.Caption = "fail"
ElseIf m > 40 And m < 66 Then
lblresults.Caption = "pass"
ElseIf m > 65 And m < 81 Then
lblresults.Caption = "credit"
ElseIf m > 80 And m < 101 Then
lblresults.Caption = "Distinction"
Else
lblresults.Caption = "Invalid input”
End if
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
EXAMPLE
Private Sub cmdcheck_Click()
If txtage.Text > 18 And txtsex.Text = "male" Then
MsgBox "adult male“
ElseIf txtage.Text > 18 And txtsex.Text = "female"
Then
MsgBox "adult female", , "result"
Else
MsgBox ("minor")
End If
End sub
If average > 75 Then
txtGrade.Text = "A"
ElseIf average > 65 Then
txtGrade.Text = "B"
ElseIf average > 55 Then
txtGrade.text = "C"
ElseIf average > 45 Then
txtGrade.Text = "S"
Else
txtGrade.Text = "F"
End If
USING OPTION AND CHECKBOX WITH IF
CONSTRUCT SELECTION CONTROL
STRUCTURE
OPTION BUTTON
Male
Award 20% of Principal amount
average = txtAverage.Text
Select Case average
Case 100 To 75
txtGrade.Text ="A"
Case 74 To 65
txtGrade.Text ="B"
Case 64 To 55
txtGrade.Text ="C"
Case 54 To 45
txtGrade.Text ="S"
Case 44 To 0
txtGrade.Text ="F"
Case Else
MsgBox "Invalid average marks"
End Select