Page 154 - Open Soource Technologies 304.indd
P. 154
Event Driven Programming
For index As Double = 0 to n Step s
This feature is new to Visual Basic 2005 and is the preferred way to declare control variables of
For... Next loops.
A variable (or simple variable) is a name to which Visual Basic can assign a
single value. An array variable is a collection of simple variables of the same
type to which Visual Basic can efficiently assign a list of values.
6.5.2 Nested For... Next Loops
The body of a For... Next loop can contain any sequence of Visual Basic statements. In particular,
it can contain another For... Next loop. However, the second loop must be completely contained
inside the first loop and must have a different control variable. Such a configuration is called
nested For...Next loops. Figure 4.5 shows several examples of valid nested loops.
For j For j For j
For k For j For j
For k
Next
For j
For k
Next
Next
For k
For k
For m
Next
Next Next
Next Next Next
Next Next Next
Figure 6.5: Nested For Next Loops.
4: The following program displays a multiplication table for the integers from
1 to 3. Here j denotes the left factors of the products, and k denotes the right
factors. Each factor takes on a value from 1 to 3. The values are assigned to
j in the outer loop and to k in the inner loop. Initially, j is assigned the value
1, and then the inner loop is traversed three times to produce the first row of
products. At the end of these three passes, the value of j will still be 1, and
the value of k will have been incremented to 4. The first execution of the outer
loop is then complete. Following this, the statement Next increments the value
of j to 2. The statement beginning “For k” is then executed. It resets the value
of k to 1. The second row of products is displayed during the next three
executions of the inner loop and so on.
148 LOVELY PROFESSIONAL UNIVERSITY