Professional Documents
Culture Documents
Control Structure
Control Structure
Control Structure
Control Statements are used to control the flow of program's execution. Visual Basic
supports control structures such as if... Then, if...Then ...Else, Select...Case, and Loop
structures such as Do While...Loop, While...Wend, For...Next etc method.
The If...Then selection structure performs an indicated action only when the condition is
True; otherwise the action is skipped.
If <condition> Then
statement
End If
The If...Then...Else selection structure allows the programmer to specify that a different
action is to be performed when the condition is True than when the condition is False.
Method 1
Method 2
e.g.: Assume you have to find the grade using nested if and display in a text box
e.g.: Assume you have to find the grade using select...case and display in the text box
A repetition structure allows the programmer to that an action is to be repeated until given
condition is true.
The Do While...Loop is used to execute statements until a certain condition is met. The
following Do Loop counts from 1 to 100.
A variable number is initialized to 1 and then the Do While Loop starts. First, the condition is
tested; if condition is True, then the statements are executed. When it gets to the Loop it
goes back to the Do and tests condition again. If condition is False on the first pass, the
statements are never executed.
number = 1
While number <=100
number = number + 1
Wend
The Do...Loop While statement first executes the statements and then test the condition
after each execution. The following program block illustrates the structure:
Do Until...Loop Statement
An example for Do Until...Loop statement. The coding is typed inside the click event of the
command button
Numbers between 1 to 1000 will be displayed on the form as soon as you click on the
command button.
The For...Next Loop is another way to make loops in Visual Basic. For...Next repetition
structure handles all the details of counter-controlled repetition. The following loop counts
the numbers from 1 to 100:
Dim x As Integer
For x = 1 To 50
Print x
Next
In order to count the numbers from 1 yo 50 in steps of 2, the following loop can be used
For x = 1 To 50 Step 2
Print x
Next
The above coding will display numbers vertically on the form. In order to display numbers
horizontally the following method can be used.
For x = 1 To 50
Print x & Space$ (2);
Next
To increase the space between the numbers increase the value inside the brackets after the
& Space$.
Following example is a For...Next repetition structure which is with the If condition used.
In the output instead of number 4 you will get the "This is number 4".
A For...Next loop condition can be terminated by an Exit For statement. Consider the
following statement block.
Dim x As Integer
For x = 1 To 10
Print x
If x = 5 Then
Print "The program exited at x=5"
Exit For
End If
Next
The preceding code increments the value of x by 1 until it reaches the condition x = 5.
The Exit For statement is executed and it terminates the For...Next loop. The Following
statement block containing Do...While loop is terminated using Exit Do statement.
Dim x As Integer
Do While x < 10
Print x
x=x+1
If x = 5 Then
Print "The program is exited at x=5"
Exit Do
End If
Loop
When properties are set for objects or methods are called, a lot of coding is included that
acts on the same object. It is easier to read the code by implementing the With...End
With statement. Multiple properties can be set and multiple methods can be called by using
the With...End With statement. The code is executed more quickly and efficiently as the
object is evaluated only once. The concept can be clearly understood with following
example.
With Text1
.Font.Size = 14
.Font.Bold = True
.ForeColor = vbRed
.Height = 230
.Text = "Hello World"
End With
Examples
a = Val(Text1.Text)
b = Val(Text2.Text)
p=1
While b >= 1
p=p*a
b=b-1
Wend
MsgBox "the value of p is = " & Str(p)
End Sub
Quiz program
Private Sub Option1_Click(Index As Integer)
Select Case Index
Case 0
ANS = "NARENDRA MODI"
Case 1
ANS = "WRONG ANSWER"
Case 2
ANS = "WRONG ANSWER"
Case 3
ANS = "WRONG ANSWER"
End Select
End Sub
Private Sub Text6_KeyPress(KeyAscii As Integer)
Dim count As Integer
count = 100
List2.Clear
If KeyAscii = 13 Then 'enter key ascii value
For start = Val(Text6.Text) To count
List2.AddItem start
Next start
End If
End Sub