A brief notes on the VB loops:
Actions mean a single line VB statement or multiple lines of VB statements
While Loop:
Syntax: / Do WhileconditionActions
Loop
Operation: /
- If the condition is true, Actions is executed. Repeat 1
Comment: / Actions may not be executed at all.
Actions may be executed infinite number of times.
Syntax: / Do
Actions
Loop Whilecondition
Operation /
- Actions is executed
- If the condition is true, Actions is executed. Repeat 2
Comment: / Actions will be executed at least once.
Actions may be executed infinite number of times.
The only difference between these two structures is:
Actions in the latter one is executed at least once.
Until Loop:
Syntax: / Do UntilconditionActions
Loop
Operation: /
- If the condition is false, Actions is executed. Repeat 1
Comment: / Actions may not be executed at all.
Actions may be executed infinite number of times.
Syntax: / Do
Actions
Loop untilcondition
Operation /
- Actions is executed
- If the condition is false, Actions is executed. Repeat 2
Comment: / Actions will be executed at least once.
Actions may be executed infinite number of times.
The only difference between these two structures is:
Actions in the latter one is executed at least once.
The key difference between While loop and Until loop is:
Fora While loop, Actions is repeated when the condition is true.
For a Until loop, Actions is repeated when the condition is false.
For Loop:
Syntax: / Forvar = begin_value extend_value [Stepstep_value]Actionstatements
Nextvar
var: the name of a variable
begin_value: constant, variable, or expression
end_value: constant, variable, or expression
step_value: constant, variable, or expression
If [ ] is omitted, the step_value is set to 1.
Operation: / There are two types of operations for the For-Next Loop
step_value > 0:
var = begin_value
Do Whilevar <= end_value
Actions
var = var + step_value
Loop / step_value < 0:
var = begin_value
Do Whilevar=end_value
Actions
var = var + step_value
Loop
comment / The number of times Actions will be repeated is finite in general.
For-Next loop can be written as Do-while loop or Do-Until Loop
Example:What is the value of I of the following program when the loop is finished?
For I = 1 to 10
I = I + 10
Next I
Answer: I = 12
Reason:
Initially I is set to 1.
Because I <= end_value (10 in this case), I = I + 10 is executed. Then I = 11.
Next I increase the value of I to 12.
Now I > end_value, the loop is finished and hence I = 12.
Why do we need so many loop structures?
- The general usage of a While loop and a Until loop is to repeat some actions when a certain condition is true (or false). The number of times of repetition is indefinite even at the run time.
- The general usage of a For loop is to repeat some actions for a given number of time. The number of times of repetition is finite and known at the run time.
Typical Example of While loop:
Suppose we have a text file “c:\marks.txt” which contains the marks of a class. How can we find the sum of these marks?
Analysis:
The basic actions to be repeated is:
read a value from the file
add the value to a variable
For-Next should not be used because the number of data in the file is not known[1].
We should use While loop or Until loop instead.
Program:
Dim a As Integer, Total As IntegerTotal = 0
Open "c:\marks.txt" For Input As 1
Do While Not (EOF(1))
Input #1, a
Total = Total + a
Loop
Close 1 / Dim a As Integer, Total As Integer
Total = 0
Open "c:\marks.txt" For Input As 1
Do Until EOF(1)
Input #1, a
Total = Total + a
Loop
Close 1
Typical Example of using For loop:
We want to repeat the action “adding 3 to the variable I” 7 times. What should we do?
We should use a For loop, because we know the number of times the action to be repeated.
Good programming practice / Not-so-good programming practiceFor j = 1 to 7
I = I + 3
Next j / J = 1
Do while J <= 7
I = I + 3
J = J + 1
loop
Note:
The RHS program is correct as well. However, what it wants to do is not as clear as that of the LHS program.
One more example of For loop:
We want to repeat the action “adding 3 to the variable I” N times, where N is the user input. We should use a For loop. Even though the number of times of the repetition is not known at the programming time, the number of repetition is known at the run time.
Assume that N is input into a textbox called txtN. Program:
N = txtN.textFor j = 1 to N
I = I + 3
Next j
[1]Hence we do not know how many times the action should be repeated.