Do and For loops provide use with several possible tools to use for looping in Visual Basic.
The Do loop is used when you do not know how many times you will need to execute a block of code. It is used when you will execute the same block of code repeatedly until something changes to indicate you are done. The Do loop is controlled by a condition.
The FOR.... NEXT loop is used when we need a counter controlled loop, which
is when we know how many times we want/need to do something.
Format:
For loopVariable = StartValue To StopValue Step StepValue
one or more VB instructions
Next loopVariable
For example, a simple For..next loop:
Loop var Start Value stop value Step Value
For intx = 1
To 10
Step 1
MsgBox (intx)
Next intx
MsgBox("the end")
What is happening?
1) The loop variable is initialized to the Start Value.
2) The loop variable is compared to the stop value. When the loop variable
becomes greater than the stop value the loop has been satisfied and control
passes to the statement following the Next intX statement.
There are a couple of ways you can think about whether the statements in side the loop are going to be executed. The statements will be execute
Until the loop variable becomes greater than the stop value
Or While the loop control variable is less than or equal to the stop value
3) If the loop variable is not greater than the stop value the instructions
inside the loop are executed.
4) At the end of the loop the loop variable is incremented by the step value.
5) Control passes back to the top of the loop to compare the loop variable to
the stop value again.
The preceding code will display all the numbers beginning with 1 and ending with
10.
--------------
The step value is optional. If it is left off the value is assumed to be 1.
Explicitly stating the loop control variable in the Next statement is optional
as well.
For intNum=
1 To 10
MsgBox (intNum)
Next
MsgBox("the end")
It is possible to have a negative step value
For intNum= 1
To 0
Step -1
MsgBox (intNum)
Next
MsgBox(intNum)
When the Step Value is negative the loop is executed until the loop variable
becomes less than the Stop Value. Notice the value of intNum after the loop is
completed is -1.
The Start Value and step value can be other values as well
For intNum=
10 To 100
Step 10
MsgBox (intNum)
Next
MsgBox(intNum)
-----------------
You must be careful when constructing For loops as with any kind of loop not
to create impossible loops. What do you think will happen with the following
loop?
For intNum=
12 To 1
Step 2
MsgBox (intNum)
Next
What do you think the result of the following loop will be?
For intNum= 1
To 10
Step 0
MsgBox (intNum)
Next
You would never consciously set a Step Value up as zero but the Step Value as
well as the Initial and Stop values can and often are variables.
A common error is to forget or incorrectly initialize the Stop Value as in the
following code. What is the result?
intBegin = 1
For intNum = intBegin To
intStop Step 1
MsgBox (intNum)
Next intNum
---------------
For……Next loops are used when you know how many times you need to execute a
block of instruction. Do loops can be also be used when you know the number of
times to repeat an action. However if you use a Do loop instead of a For…Next
loop when you know how many times you need to execute a block of code you are
responsible for keeping track of how many times you have executed the
conditional block of code.