Visual Basic Newbie Tutorial : For...Next Loop (For Newbie)
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!
Original Author: Lam Ri Hui
Code
Visual Basic Newbie Loop There are two types of loops : Counter Loops A counter loop is also known as a For loop At the beginning of a The syntax of the loop is as follows For countervariable = start To The first time the loop is run, the counter Example The following program will cause the computer to For b = 1 To 5 The variable used is b. It stands for the first Thus, the above code is same as following : Beep Step Size To change the step size, include step in the For Example Dim Count For Count = 0 To 100 Step 10 The output will as follow : 0 Nested Loops The following example shows two nested loops: For x = 1 To 3 The inner loop is performed first. After it is Inner Loop Outer Loop ##### The Print statement by itself starts a new line Dim x As Integer, y As Integer For x = 1 To 3 For y = 1 Print Nested loops need not be hard to use. Just
Tutorial 1 : For...Next 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.
· Counter Loop - where repetition is based on a specified number of times
· Conditional Loop - where repetition is based on set conditions
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.
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.
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
end
Statements to be executed
Next countervariable
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.
send out five beeps, one after another from the computer's speaker.
Beep
Next b
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.
Beep
Beep
Beep
Beep
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.
loop. You can use any number, including decimals and negative numbers for the
step size.
Print Count
Next Count
10
20
30
40
50
60
70
80
90
100
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.
<------------------------------
|
For y = 1 To 5 <---
|
Print "#"; |
Inner loop
| Outer loop
Next y ------------
|
|
Print
|
Next x ---------------------------------------
completed, then the outer loop carries on.
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 #####.
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
#####
#####
for each row of #####, then include another Print Statement.
To 5
Print "#";
Next y
Print
Next x
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
----------------------------------
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.