VB Lecture Notes - For Loops
Objective #1: Use For loops.
- A For loop allows you to repeat a section of code many times over and over again. A For loop is called
a definite loop (or determinate loop) because the programmer knows exactly how many times the code will be repeated. Each pass of the loop is called an iteration.
If you know exactly how many iterations are necessary for a certain task, you should use a For loop rather than another kind of loop that you will study later called
an indefinite loop.
- Example:
For J = 1 To 100
MessageBox.Show("Hello World")
Next
This loop will iterate exactly 100 times. Each time the loop iterates the computer will display a message box due to the statement that is inside of the body of
the loop. The body of a loop is the set of statement(s) that is indented inside of the loop between For and Next. The variable J is
called a loop variable. It begins with the value of 1. Each time the loop iterates the J increases
by one. So J will be equal to 2 the second time around the loop and so on. When J is equal to 100 the loop will
iterate one last time. In the example above, the initial value of J is 1. Notice that the body of the For loop is indented.
Also realize that you can place as many statements in the body of the loop as you wish. Usually, the loop variable of a For loop is an Integer rather
than a Double.
It is tradition and considered fine style to use uppercase or lowercase J or I as the name for a loop variable.
You can declare the loop variable within the loop as in the example below:
For J As Integer = 1 to 100
MessageBox.Show("Hello World")
Next
but J can only be used inside the For loop if it is declared on the first line of the loop. It is "local" to the loop.
- The loop variable can change by amounts other than 1 on each iteration. The amount that the loop variable changes is called the loop's step size. The default step size for
Visual Basic For loops is 1. However, you can type the keyword Step followed
by any other value to change the step size.
Examples:
For J = 1 To 100 Step 12
MessageBox.Show("Hello World")
Next
For J = 100 To 1 Step -1
MessageBox.Show("Hello World")
Next
For dblValue = 0 To 1 Step 0.1
MessageBox.Show("Hello World")
Next
For J = 2 To 11 Step 2
MessageBox.Show("Hello World")
Next
Determine the number of loop iterations and the final value of the loop variable in each example above. It is important that you know how to trace a loop. When you trace a loop, you carefully follow
and keep track of every variable and record the successive values of the variables in a column.
- If you use a step size of 1, do not type out the "Step 1" part of the For statement. You will lose points
on programs that include this unnecessary code. It is considered to be acceptable style to use loop variables that consist of a name with one letter such as J, j, I,
or i.
- If a For loop has a Step value of 1 as in
For J = Low To High
intIterations = intIterations + 1
Next
then the number of loop iterations can easily be calculated with the formula
# of loop iterations = High - Low + 1
Be wary of the fencepost error which results from not adding 1 in this formula. The fencepost error is dangerous since it causes computer programmers to underestimate the overall number
of loop iterations by one. If you built a fence with 4 fencerails then you need 5 fenceposts. That is, you need one more fencepost than fencerails. The fencepost error is also known as an off-by-one-bug
(OBOB).
- If you would like, you may declare the loop variable within the For loop. For example,
For j As Integer = 0 To 10
MessageBox.Show("Hello World")
Next j
is equivalent to
Dim j As Integer = 0
For j = 0 To 10
MessageBox.Show("Hello World")
Next j
except for the fact that you cannot use the loop variable j outside of the loop.
- Be careful not to create infinite loops that iterate forever. The following For loop may iterate forever
For i = 1 To 10 Step -1
Objective #2: Trace double-nested For loops.
- You will not be tested on the following material however it is worthwhile to understand double-nested loops.
- A nested For loop is a For loop
inside of another For loop.
The two loops together are called double-nested For loops. Example:
Dim R As Integer = 0
Dim C As Integer = 0
For R = 1 To 3
For C = 1 To 2
lblOutput.Text = lblOutput.Text + R * C
Next
lblOutput.Text = lblOutput.Text + vbCrLF
Next
Notice how the nested For loop is indented inside of the outer For loop for good style. Also notice how blank lines are placed around loops for good style and increased
readability.
We will trace this example in class.