VB Control Structures

Introduction
There are three types of VB Program controls namely: Sequential, Selective and Repetitive.

Sequential
This is where program codes are executed one after the other, from the first to the last (from top to bottom).

Example: private sub cmdCalculate_Click Rem Beginning of code Const pie = 3.142 Dim radius As Integer, area As Double area = pie * radius^2 print "The Area is = ", area end sub

Selective / Branching
These control structures contain statements whose execution is determined by the results of a condition that is embedded within the structure. In this case, program first tests the condition and if it evaluates to true then the succeeding action (code) is executed. Examples include: Simple if, if ... else, if .. elseif, select case

'''if ... then ... end if statement'''

This statement has only one option which is executed when the condition evaluates to true. Otherwise the program does nothing (terminates), action is skipped and there is no alternative action to be taken. Syntax:

if (condition) then

Statements end if

Example:

private sub cmdCompute_Click Rem Beginning of code Dim feeDue, feePaid, feeBalance As Integer Rem Assigning a value to Fee Due feeDue = 30000.00 ' Prompting the user to enter the fee paid by a student feepaid = InputBox("Enter the Fee Pid by Student") ' Computing the fee balance feeBalance = feeDue - feePaid Rem Beginning of if statement if (feeBalance >0) then Rem displaying the remark in a picture box picRemark.print "You have a Fee Balance of ", feeBalance end if ' End of if statement end sub

'''if ... then ... else ... end if statement'''

This statement has two options (parts). The first is executed when the condition evaluates to true while the statements in the else part are executed when the condition results to false.

Syntax:

if (condition 1) then

Statement 1

else Statement 2 end if

Example:

private sub cmdCompute_Click Rem Beginning of code Dim feeDue, feePaid, feeBalance As Integer Rem Assigning a value to Fee Due feeDue = 30000.00 ' Prompting the user to enter the fee paid by a student feepaid = InputBox("Enter the Fee Pid by Student") ' Computing the fee balance feeBalance = feeDue - feePaid Rem Beginning of if statement if (feeBalance >0) then Rem displaying the remark in a picture box picRemark.print "You have a Fee Balance of ", feeBalance else picRemark.print "You have a no Fee Balance" end if ' End of if statement end sub

'''if ... then ... elseif ... end if statement'''

The program has a variety of choices for possible action. Each action has a condition attached to it and determined by the result of the condition after being evaluated. If one of the actions is evaluated and returns true, all the other remaining choices are ignored.

If a condition is tested and evaluates to true, then the corresponding statements (actions)are executed.

Syntax:

if (condition 1) then Statement 1 elseif (condition 2) then Statement 2 elseif (condition 3) then Statement 3 .     .      . elseif (condition n) then Statement n else Statement X 'Executed when all evaluate to false end if Example:

private sub cmdCompute_Click Rem Beginning of code Dim feeDue, feePaid, feeBalance As Integer Rem Assigning a value to Fee Due feeDue = 30000.00 ' Prompting the user to enter the fee paid by a student feepaid = InputBox("Enter the Fee Pid by Student") ' Computing the fee balance feeBalance = feeDue - feePaid Rem Beginning of if statement if (feeBalance >0) then Rem displaying the remark in a picture box picRemark.print "You have a Fee Balance of ", feeBalance elseif (feeBalance < 0) then picRemark.print "You have overpayed fees by ", feeBalance else picRemark.print "You have a no Fee Balance" end if ' End of if statement end sub

Select case statement

Like the nested if, this statement is also applied where a decision has to be made from many choices. Its execution is faster that the Nested if (and if .. elseif..else..end if) statement.

Syntax:

select case(variablename) case value1: statement 1 case value2: statement 2 case value3 To value6, value8 To value10: statement 3 case value7, value8: statement 4 .         .          .   case valuen: statement n   case else: statement x ' Is execeted when all cases return false End select

Example: private sub cmdCompute_Click On Error Resume next Rem Beginning of code Dim cat1, cat2, exam, score As Integer Rem Assigning a value to Fee Due cat1 = inputBox("Enter Score for Cat 1") cat2 = inputBox("Enter Score for Cat 2") exam = inputBox("Enter Score for Cat 1") score = ((cat1 + cat2)*30/100) + (exam * 30/100) Select case(score) case 75 To 100: Print "Distinction" case 60 To 74: Print "Credit" case 40 To 59: Print "Pass" case 30 To 39: Print "Referred" case 0 To 29: Pring "Fail" case else: Print "Score out of Range" End Select end sub

VB GoTo Statement
It is a Visual Basic brancing statement, mostly used in Run-Time error trapping.

Syntax: GoTo Label Label is a labeled line. Labelled lines are formed by typing the Label (identifier) followed by a colon.

Example: Line 10 statements GoTo Line10 When the code reaches the GoTo statement, the program control is transferred to the line labeled Line10

Repetitive
This is where the program code is continuously executed until a certain condition is satisfied. There are two types of repetitive control structures: Pre-condition control structures and Post-condition control structures

Pre-condition control structures:
In this case, the condition is first evaluated before the statements within the structure are executed. The minimum number of times that this structure can be execute is Zero.Examples include: for, while do..while .. loop, do..until.. loop.

For statement
This statement first tests the condition and then continuously executes until the condition evaluates to false. This statement has four parts: countervariable initialization, condition, terminating (final) value and increment/decrement (step).

Syntax:

For countervariable = initial value to final value step statements Next countervariable Example: for x = 1 to 10 step 1 for y = 1 to 10 step 1 print " @" next y  print: next x

While..wend statement
This statement also first tests the condition and then continuously executes until the condition evaluates to false. When applying this statement, you need to initialize the counter before beginning of the loop (above the loop), then increment/ decrement the counter within the loop.

Syntax:

counter initialization while (condition) statements counter increment/decrement wend Example: private sub cmdEven_Click 'program to generate even numbers Dim i As Integer i = 1 while (i<1000) If (i Mod 2 = 0) Then print i  End If   i = i + 1 wend End Sub

Do While:
It executes continuously as long as the condition is evaluated and returns true. This loop is used when the number of times the loop is to execute is not known.

Syntax: Do While (condition) statements loop Example 1: Do While Private Sub Form_Load 'display the numbers from 0 to 9 Dim n As Integer 'method 1 n = 1 ' initialize n   Do While (n < 10) Text1.Text = Text1.Text & i & "," n = n + 1 Loop End Sub

Do Until:
It executes continuously as long as the condition is evaluated and returns false. This loop is used when the number of times the loop is to execute is not known.

Syntax: Do Until(condition) statements loop Private Sub Form_Load 'display the numbers from 0 to 9 Dim n As Integer n = 0 'initialize n   Do Until (n > 10) Text2.Text = Text2.Text & i & "," n = n + 1 Loop

End Sub

Post-Condition Structures
These are structures whose condition is evaluated after the statements within them have been executed. These type of statements must execute at least once.

Do While:
It executes continuously as long as the condition is evaluated and returns true. This loop is used when the number of times the loop is to execute is not known.

Syntax: Do  statements loop While (condition) Example 1: Do While Private Sub Form_Load 'display the numbers from 0 to 9 Dim n As Integer 'method 1 n = 1 ' initialize n   Do         Text1.Text = Text1.Text & i & "," n = n + 1 Loop While(n < 10) End Sub

Do Until:
It executes continuously as long as the condition is evaluated and returns false. This loop is used when the number of times the loop is to execute is not known.

The minimum number of times that the loop can execute is one.

Syntax: Do  statements loop Until(condition) Private Sub Form_Load 'display the numbers from 0 to 9 Dim n As Integer n = 0 'initialize n   Do         Text2.Text = Text2.Text & i & "," n = n + 1 Loop Until (n > 10)

End Sub