Visual Basic Lesson 9 - Looping
Visual Basic Lesson 9 - Looping
Lesson 9: Looping
<Previous Lesson> <<Home>>< Next Lesson>
Ads by Google ► Visual Basic Code ► VB Net ► VB 2010 ► For Loop
In lesson 7 and lesson 8, we have learned how to handle decisions making process using If...Then...Else and also Select Case program
structures in Visual Basic. Another procedure that involves decisions making is looping. Visual Basic allows a procedure to be repeated many
times until a condition or a set of conditions is fulfilled. This is generally called looping . Looping is a very useful feature of Visual Basic
because it makes repetitive works easier. There are two kinds of loops in Visual Basic, the Do...Loop and the For.......Next loop.
9.1 Do Loop
a) Do While condition
Loop
b) Do
Block of one or more VB statements
Loop While condition
c) Do Until condition
Block of one or more VB statements
Loop
d) Do
Block of one or more VB statements
http://www.vbtutor.net/lesson9.html 1/5
2/4/2016 Visual Basic lesson 9: Looping
New Aadhar
Card Online
Example 9.1
num.Text=counter
counter =counter+1
Loop
Do
num.Text=counter
counter=counter+1
Sometime we need exit to exit a loop earlier when a certain condition is fulfilled. The keyword to use is Exit Do. You can examine Example 9.2
for its usage.
Example 9.2
Do
n=n+1
http://www.vbtutor.net/lesson9.html 2/5
2/4/2016 Visual Basic lesson 9: Looping
Sum = Sum + n
If n = 100 Then
Exit Do
End If
Loop
End Sub
Explanation
In the above example, we compute the summation of 1+2+3+4+……+100. In the design stage, you need to insert a ListBox into the form for
displaying the output, named List1. The program uses the AddItem method to populate the ListBox. The statement List1.AddItem "n" & vbTab
& "sum" will display the headings in the ListBox, where it uses the vbTab function to create a space between the headings n and sum.
Next
Example 9.3 a
For counter=1 to 10
display.Text=counter
Next
Example 9.3 b
counter=counter+1
Next
Example 9.3 c
counter=counter10
Next
Sometimes the user might want to get out from the loop before the whole repetitive process is executed, the command to use is Exit For. To
exit a For….Next Loop, you can place the Exit For statement within the loop; and it is normally used together with the If…..Then… statement.
Its usages is shown in Example 9.3 d.
Example 9.3 d
http://www.vbtutor.net/lesson9.html 3/5
2/4/2016 Visual Basic lesson 9: Looping
Private Sub Form_Activate( )
For n=1 to 10
If n>6 then
Exit For
End If
Else
Print n
End If
End Sub
When you have a loop within a loop, then you have created a nested loop. You can actually have as many loops as you want in a nested
loop provided the loops are not the neverending type. For a nested loop that consists of two loops, the first cycle of the outer loop will be
processed first, then it will process the whole repetitive process of the inner loop, then the second cycle of the outer loop will be processed
and again the whole repetitive process of the inner loop will be processed. The program will end when the whole cycle of the outer loop is
processed.
Next counter2
Next counter1
Example 9.4
Print “Hello”
For secondCounter=1 to 4
Next secondCounter
Next firstCounter
http://www.vbtutor.net/lesson9.html 4/5
2/4/2016 Visual Basic lesson 9: Looping
End Sub
Figure 9.1
The output of the above program is shown in Figure 9.1. As the outer loop has five repetitions, it will print the word “Hello” five times. Each time after it
prints the word “Hello”, it will print four lines of the “Welcome to the VB tutorial” sentences as the inner loop has four repetitions.
The structure of a While….Wend Loop is very similar to the Do Loop. it takes the following form:
While condition
Statements
Wend
The above loop means that while the condition is not met, the loop will go on. The loop will end when the condition is met. Let’s examine the
program listed in example 9.4.
Example 9.5
n=n+1
Sum = Sum + n
Wend
End Sub
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: [email protected]
[Privacy Policy]
http://www.vbtutor.net/lesson9.html 5/5