Page 148 - Open Soource Technologies 304.indd
P. 148
Event Driven Programming
lstTable.Items.Add(i & “ “ & i ^ 2)
i += 1 ‘Add 1 to i
Loop
End Sub
In general, a portion of a program of the form
initial value
For i = m to n terminating value
control variable
statement (s) boby
Next
constitutes a For... Next loop. The pair of statements For and Next cause the statements between
them to be repeated a specified number of times. The For statement designates a numeric
variable, called the control variable, that is initialized and then automatically changes after each
execution of the loop. Also, the For statement gives the range of values this variable will assume.
The Next statement increments the control variable. If m n, then i is assigned the values m,
m + 1, ..., n in order, and the body is executed once for each of these values. If m > n, then the
body is skipped and execution continues with the statement after the For... Next loop.
When program execution reaches a For... Next loop, such as the one shown previously, the For
statement assigns to the control variable i the initial value m and checks to see whether i is
greater than the terminating value n. If so, then execution jumps to the line following the Next
statement. If i<=n, the statements inside the loop are executed. Then, the Next statement increases
the value of i by 1 and checks this new value to see if it exceeds n. If not, the entire process is
repeated until the value of i exceeds n. When this happens, the program moves to the line
following the loop.contains the pseudocode and flowchart of a For ... Next loop.
The loop does not stop until counter has passed end. If counter is equal to end,
the loop continues. The comparison that determines whether to run the block
is counter <= end if step is positive and counter >= end if step is negative.
142 LOVELY PROFESSIONAL UNIVERSITY