VB Control Structures

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

CONTROL STRUCTURES IN

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.

A control structure is a container for a


series of function calls, instructions and
statements.
Importance of control
structures
Are very useful in writing code that gives
flexibility to the programmer.

Can be used to manipulate data by using
logic to specify what should be done when
a user tries to change or modify the data.
Gives power to the programmer because
the programmer can specify when a
situation occurs, the function should do this
or that.
TYPES OF CONTROL
STRUCTURES
There are three programming construct/ control
structure in visual basic
1) sequential
2) looping /iteration
3) decision/selection
SEQUENTIAL
This involves executing program statement in
the order they appear One after the other

A control structure that executes statements


in a program procedurally; one after another.


Private Sub Form_Load()
Form1.Show {first to execute}
Print "Hello” {second to execute}
End Sub
DECISION/SELECTION

The selection control structure allows one set of


statements to be executed if a condition is true
and another set of actions to be executed if a
condition is false.

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.

 Use if.. Then when your program must select


either /decision

It evaluates whether a condition is true or false

syntax:
If {condition} then
{statements}
End if
Example program to determine
student age
Pseudo code/Algorithms

Declare the variables i.e. age and message



Initialize variable age

If age is greater than or equal to 18 then

Display “you are a grown up. Learn to be
responsible.”

program
Private sub cmddisplay_click()

Dim age as integer

Dim message as string

Age=value(txtage.text)

If txtage.text>=18 then

Txtmessage.text=”You are a grown up. Learn to be responsible”

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

NB: Make use of If...Then..Else Statement


If..Then ..Else if..else
 Used when more than two alternatives are
to be selected by your program

Nested If...Then...Else selection structures


test for multiple cases by placing
If...Then...Else selection structures inside
If...Then...Else structures.
syntax
If < condition 1 > Then
statements
ElseIf < condition 2 > Then
statements
ElseIf < condition 3 > Then
statements
Else
Statements
End If
If (condition1) Then
Statement1
Else if (condition2) Then
Statement2
Else if (condition3)
Statement3
:
.
Else
StatementN
End if
Example program
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


Interface
program
Private Sub cmdcompare_Click()
Dim position As Integer
position = Val(txtpos.Text)
If (position >= 1) And (position <= 3) Then
picresult.Print "top 3 student"
ElseIf (position >= 4) And (position <= 5) Then
picresult.Print " top 5 student"
ElseIf (position >= 6) And (position <= 10) Then
picresult.Print "top 10 student"
Else
picresult.Print "You are not categorized"
End If
End Sub
ASSIGNMENT
Write a program that will evaluate two text
fields txtage and txtgrade (grade can be 1 to 5)
If age>10 and grade=2 it should print you are
too old to be at this grade
If age=3 and grade=5 it should print you are too
young to be at this grade else it should print
invalid input. Make use of If..Then..ElseIf
decision structure.
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
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

-Control used to make a single selection


out of several options available.

VALUE PROPERTY
The value property is used to indicate
whether the option button is selected or
not.
A value property of TRUE means the
option button is selected and a value
property of FALSE means no selection
made.
PROGRAM EXAMPLE:

Program to prompt user to enter principal


amount, calculate and display interest based
on the gender selected. The criteria for interest
rates are given in the table below.
GENDER
INTEREST RATE

Male
Award 20% of Principal amount

Female Award 15% of principal amount


program
 Private Sub cmdcompute_Click()
 Dim Amount As Double, interest As Double
 Amount = Val(txta.Text)
 Picresult.Cls
 If (Optmale.Value = True) And (Optfemale.Value = False) Then
 interest = 20 / 100 * Amount
 Picresult.Print "Interest = ", interest
 ElseIf (Optfemale.Value = True) And (Optmale.Value = False) Then
 interest = 15 / 100 * Amount
 Picresult.Print "Interest = ", interest

 Else
 MsgBox "Please select Gender" End If
 End Sub
Checkbox
-Control used to make more than one
selection out of several options available.

VALUE PROPERTY
The value property is used to indicate
whether the checkbox is selected or not. A
value property of 1 means the checkbox is
selected and a value property of 0 means
no selection made.
PROGRAM EXAMPLE:

Program to prompt user to enter salary


earned, calculate and display tax based on
the status selected. The criteria for tax
rates are given in the table below.
STATUS TAX RATE
Married only 20% of salary

Studying only 15% of salary

Married and Studying 10% of salary


User interface
Private Sub CMDCOMPUTE_Click()
Dim SALARY As Double, tax As Double
SALARY = Val(TXTS.Text)
PicResult.Cls
If (Chkm.Value = 1) And (chkS.Value = 0) Then
tax = 20 / 100 * SALARY
PicResult.Print "Tax = ", tax
ElseIf (chkS.Value = 1) And (Chkm.Value = 0) Then tax = 15 /
100 * SALARY
PicResult.Print "Tax = ", tax
ElseIf (Chkm.Value = 1) And (chkS.Value = 1) Then tax = 10 /
100 * SALARY PicResult.Print "Tax = ", tax
Else
MsgBox "Please select status"
End If
End Sub
Select case
 If you have a lot of conditional statements, using If..Then..Else
could be very messy. For multiple conditional statements, it is
better to use Select Case
The format is :
Select Case expression
Case value1
Block of one or more VB statements
Case value2
Block of one or more VB Statements
Case value3
Block of one or more VB statements
Case value4
Case Else
Block of one or more VB Statements
 End select
Select Case Index
Case 0
Statements
Case 1
Statements
End Select
Example of a grading system
 Dim c As Integer
c = InputBox("Enter student marks", "marks")
Select Case c
Case 0 To 40
lblresults.Caption = "fail"
Case 41 To 65
lblresults.Caption = "pass"
Case 66 To 80
lblresults.Caption = "credit"
Case 81 To 100
lblresults.Caption = "Distinction"
Case Else
lblresults.Caption = "Invalid input”
 End select
 Dim average as Integer

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

You might also like