by Lam Ri Hui (26 Submissions)
Category: Coding Standards
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
Hi VB newbie, do you know how to use for...next loop? If you don't, never mind. Read this tutorial to learn about that. You will learn how to use for...next loop by explanation and examples by me. Leave comments about this tutorial. Happy coding!
Visual Basic Newbie
Tutorial 1 : For...Next Loop
Loop
A loop is used to run a block of statements over and over. It is simply a group
of commands that is repeated a specified number of times or for a specified
length of time. An example of looping action : imagine you are sticking mail
labels to several invitation cards. For each card, you check that the label
matches the name on it, then you tick it off from your guest list, before
pasting the mail label onto the card. You then proceed to the next card, repeat
the check, tick and paste actions, and so on. This is essentially how a loop
works.
There are two types of loops :
· Counter Loop - where repetition is based on a specified number of times
· Conditional Loop - where repetition is based on set conditions
Counter Loops
You use a counter loop when you want the computer to perform a task for a
specific number of times. This is similar to running a track race of, say 12
laps. While running, you count the number of laps, and when you have completed
12, you stop.
A counter loop is also known as a For loop
or a For/Next loop. This is because the ends of the loop are
defined by the For
statement and the Next
statement. A For/Next loop requires two statements: the
For statement at the
beginning of the loop and the Next
statement at the end of loop.
At the beginning of a
For loop, you define
a counter variable as well as the start and end values for the variable. For
example, if you want the loop to repeat 12 times, you would set
For X = 1 To 12
The syntax of the loop is as follows
For countervariable = start To
end
Statements to be executed
Next countervariable
The first time the loop is run, the counter
variable is set to the value of the starting point (usually 1). After the
statements are executed once, the counter variable reaches the Next statement
where a counter registers a count of 1. The counter variable increase by one for
each loop. Each time, the value in the counter is checked against the value of
the end point. It stops when this value is reached.
Example
The following program will cause the computer to
send out five beeps, one after another from the computer's speaker.
For b = 1 To 5
Beep
Next b
The variable used is b. It stands for the first
number in a For loop. Each time the loop is executed, the counter variable b
increases by 1, until it reaches a value of 5.
Thus, the above code is same as following :
Beep
Beep
Beep
Beep
Beep
Step Size
The default change in the loop counter is one. You can specify a different value
for the change. This value is known as the step size. Referring to
our example of track, if the track is 1000 meters long, a race of 10,000 meters
will require 10 laps. On a track of 500 meters, it would require 20 laps. The
step size changes the number of required laps or runs.
To change the step size, include step in the For
loop. You can use any number, including decimals and negative numbers for the
step size.
Example
Dim Count
For Count = 0 To 100 Step 10
Print Count
Next Count
The output will as follow :
0
10
20
30
40
50
60
70
80
90
100
Nested Loops
You can nest two or more For loops inside one another. Whenever you program
needs to repeat a loop more than once, use a nested loop.
The following example shows two nested loops:
For x = 1 To 3
<------------------------------
|
For y = 1 To 5 <---
|
Print "#"; |
Inner loop
| Outer loop
Next y ------------
|
|
Print
|
Next x ---------------------------------------
The inner loop is performed first. After it is
completed, then the outer loop carries on.
Inner Loop
In the above example, the inner loop starts with the variable
y = 1. It prints #.
The semicolon will cause the next statement to print on the same line. Hence the
second time the loop is carried out another # will be printed next to the
first, resulting in ##. After the inner loop is repeated 5 times, the result
will be #####.
Outer Loop
The outer loop begins with the variable x = 1.
This will cause the inner loop to occur once, resulting in #####. The second
time the outer loop occurs, another ##### will be printed, and so on. Thus after
3 repetitions of the outer loop the result will be
#####
#####
#####
The Print statement by itself starts a new line
for each row of #####, then include another Print Statement.
Dim x As Integer, y As Integer
For x = 1 To 3
For y = 1
To 5
Print "#";
Next y
Print
Print
Next x
Nested loops need not be hard to use. Just
remember the rule : The inner loop must be completed before the Next statement
for the outer loop is encountered. Use indentation and blank lines between loops
to make them easy to read and debug.
----------------------------------
Visual Basic Newbie Tutorial 1 : For...Next Loop Ends Here
----------------------------------