For...Next Statement


For...Next Statement

Syntax

     For counter [As datatype] = start To end [Step step]        [statements]        [Exit For]        [statements]        [Continue For]     [statements]     Next [counter] 


counter (required in For clause; numeric variable)

A variable that serves as the loop counter.


datatype (optional)

New in 2003. The data type of counter when including the declaration in the For clause.


start (required; numeric expression)

The starting value of counter for the first iteration of the loop.


end (required; numeric expression)

The maximum limit of counter (or minimum limit, if step is negative) during its iterations.


step (optional; numeric expression)

The amount by which counter is to be incremented or decremented on each iteration of the loop. If omitted, the default value is 1.


statements (optional)

Lines of program code to execute within the loop.

Description

The For...Next statement defines a loop that executes a given number of times, as determined by a loop counter.

To use the For...Next loop, you must assign a numeric value to a counter variable. This counter is either incremented or decremented automatically with each iteration of the loop. In the For statement, you specify the value that is to be assigned to the counter initially and the maximum value the counter will reach for the block of code to be executed. The Next statement marks the end of the loop. The Exit For statement can be used at any time to exit the loop immediately.

New in 2005. The Continue For statement can be used at any time to immediately jump back to the top of the loop and attempt to process the next iteration. The counter variable is adjusted by step and is reevaluated immediately upon reaching the top of the loop.

Usage at a Glance

  • Normally, counter is an integral data type. However, it can be any data type that supports the following operators: less than or equal to (<=), greater than or equal to (>=), addition (+), and subtraction (-). Beginning with Visual Basic 2005, this can include any class or structure, as long as these operators have been overloaded to support the specified class. counter cannot be a Boolean variable or an array element.

  • The values for start, end, and step can be positive, negative, or zero. They are evaluated only the first time through the loop. If you change their values in the loop's code, it has no impact on the number of iterations.

  • If end is less than start and no Step keyword is used, or the step counter is positive, the For...Next loop is ignored and execution commences with the first line of code immediately following the Next statement.

  • If start and end are equal and step is 1, the loop will execute once.

  • The For...Next loop can contain any number of Exit For statements. When the Exit For statement is executed, program execution continues with the first line of code immediately following the Next statement.

  • For...Next loops can be nested, as shown here:

         For eachDay = 1 to 365        For eachHour = 0 to 23           For eachMinute = 0 to 59              ...code here...           Next eachMinute        Next eachHour     Next eachDay 

  • You should avoid changing the value of counter in the code within the loop, as this can lead to unexpected results.

  • Once the loop has finished executing, the value of counter is officially undefined. That is, you should not make any assumptions about its value outside of the For...Next loop, and you should not use it unless you first reinitialize it.

Example

The following code adds up all of the values in an array.

     Public Function SumArray(ByVal sourceArray(  ) As Integer) As Integer        Dim counter As Integer        Dim newTotal As Integer = 0        For counter = LBound(sourceArray) To UBound(sourceArray)           newTotal += sourceArray(counter)        Next counter        Return counter     End Function 

The following code block does the same thing, but in reverse order.

     For counter = UBound(sourceArray) To LBound(sourceArray) Step -1        newTotal += sourceArray(counter)     Next counter 

Version Differences

  • Visual Basic .NET 2003 adds the As clause to the For statement for inline counter declaration.

  • Visual Basic 2005 includes the Continue For statement.

See Also

For Each...Next Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net