|
|
|
We studied few looping structures in the last article. Today we take a look at the arguably most frequently used looping structure in any programming language, the For-Next loop. For...Next LoopsCounter loops are also known as For or For...Next loops. They're called For...Next loops because the beginning and end of the loop are defined by the For statement and the Next statement, respectively. The syntax of a For...Next loop is For Counter = Start To End [Step Noofsteps] statements Next [Counter]Following points are to be considered
At the beginning of a For...Next loop, you define a counter variable, as well as the beginning and end points of the variable's value. The first time the loop is run, the counter variable is set to the value of the beginning point. Each time the program runs through the loop, the value of the counter increments. If the Step keyword is used, the counter variable increments as dictated by the number following the Step keyword. For example, in the following statement, Cntr will increment by 2: For Cntr = 0 To 10 Step 2As the counter variable increments, it's checked against the value of the end point. If the counter is larger than the end point, the program skips out of the loop and onto the first statement following the loop. If the beginning value of the loop is greater than the ending value, the loop won't execute at all, unless you set up the loop to count backward. For example, the following loop doesn't cause an error, but because the start boundary of the loop (9) is greater than the end boundary (0), the loop is ignored and the message box doesn't appear: For-Next loops can be terminated early using a conditional statement inside the loop. This statement uses an Exit For statement. Now that we have studied conditional and looping structures, we are fully equipped to start our next series on arrays.
Go To Page: 1
The copyright of the article Looping Structures II in Learning Visual Basic is owned by . Permission to republish Looping Structures II in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|