Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 7: Repetition
Last change: February 13 2016 18:47:47.
<<PreviousNext>>

7.2 The For...Next Structure

The preceding section focused on the Do…Loop structure, and briefly mentioned the While…End While structure. Both of these structures perform the repetition based on the value of a condition (True or False). This section discusses the For…Next structure that performs repetition based on a counter. The For...Next structure has the following syntax:

For Counter = Starting Value To Ending Value [Step Increment] Statements to be executed Next [Counter]

where Counter = a variable to serve as the counter in the repetition; it starts with the starting value and is added by the increment in each iteration.
Starting Value = an expression that can be evaluated to a numeric value, which will be assigned to Counter when the loop starts.
Ending Value = an expression that can be evaluated to a numeric value, which will be compared with Counter.
Increment = an optional parameter that will be added to Counter for each iteration. Increment can be either a positive or a negative value; if this parameter is omitted, 1 is the default value.

As the syntax shows, the For...Next structure starts a loop with the For statement and ends with the Next statement. The counter in the Next statement is optional.
When the loop starts, the counter is set to the starting value for the first iteration. The increment is then added to the counter for each of the subsequent iterations. At the beginning of each iteration, the counter value for the upcoming iteration is first compared with the ending value. Either the statements inside the loop will be executed, or the loop will terminate, depending on the sign of the increment and the result of comparison.

A Closer Look at the Increment

Notice that the increment can be either positive or negative. If the increment is positive, the loop will end as soon as the counter is greater than the ending value. If the starting value is greater than the ending value, the loop will terminate immediately and the statements inside the loop will never be executed.
On the other hand, if the increment is negative, the loop will end as soon as the counter is less than the ending value. In this case, if the starting value is less than the ending value, statements inside the loop will never be executed. The two flowchart fragments in Figure 7.5 show how the For...Next loop works with positive and negative increment.

Figure 7-5
For loops with positive and negative increment

Notice that when the increment is negative, adding it to the counter will decrease the value of the counter. The following examples illustrate how the For...Next loop can be used.

Last change: February 13 2016 18:47:47.
  1. Example 1. Showing a Sequence of Numbers
  2. Example 2. Reading a Name List and Populating a List Box
  3. The Endless Loop
  4. Do Loop Without a Condition on Either Statement
  5. Example 3. Computing the Value of an Infinite Series
  6. Example 4. Finding the Solution to an Equation Numerically
  • 7.2 The For...Next Structure
    1. Example 5. Listing a Sequence of Numbers
    2. Example 6. Listing Who’s Invited (the Invitation Project)
    3. Example 7. Every Other Day
    4. Example 8. Displaying a String on Two Lines (the ShowTwoLines Project)
    5. Nesting the Loops
    6. Example 9. Matching Up Teams in a League
    7. Example 10. What Is Your Chance to Win?
    8. The For Each...Next Structure
    9. <<PreviousNext>>