| The For loop is probably the most
popular loop statement in Visual Basic
, here is the syntax of the For loop.
For index = start To
end [Step step]
[ Statements ]
[ Exit For ]
Next [index]
The For is the keyword you use to
start the loop.
Index is a user defined numeric
variable that the loop uses as a counter
.
start is the number from which
the loop starts from .
To is the keyword which separates
the start and end numbers .
end is the number at which
the loop stops .
Step is an optional keyword indicating
the loop should step .
step is the size of increment
/ decrement the step should have (this
can be a negative number)
Next is the keyword that completes
the loop .
index is used to identify which
index is updated by the Next keyword
.
Here are some general points about
For loops that I have read in books
that you may want to use .
- The index value is usual called
something clear like intCounter
.
- The index after the Next is optional
but helps reading of your code.
- Likewise if your Step value is
1 you can omit the Step 1 from the
end of your because the default
value is 1 , but it is best kept
for clarity .
Lets look at some samples , none
of these require any extra controls
(note set the AutoRedraw property
to True) . Enter the following code
in the Form Load event.
example 2
example 3
These examples will give you a general
idea of the For loop
|