For...Next Statement

   
For...Next Statement

Syntax

 For   counter   =   initial_value   To   maximum_value _   [Step stepcounter]   'code to execute on each iteration   [Exit For] Next [   counter   ] 
counter (required (optional with Next statement); any valid numeric variable)

A variable that serves as the loop counter

initial_value (required; any valid numeric expression)

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

maximum_value (required; any valid numeric expression)

The value of counter during the last iteration of the loop

stepcounter (optional (required if Step is used); any valid numeric expression)

The amount by which counter is to be incremented or decremented on each iteration of the loop

Description

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 block of code that is to execute repeatedly, and it also serves as a kind of flag that indicates that the counter variable is to be modified.

Rules at a Glance

  • If maximum_value is greater than initial_value 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 initial_value and maximum_value are equal and stepcounter is 1, the loop will execute once.

  • counter cannot be a Boolean variable or an array element.

  • counter is incremented by one with each iteration unless the Step keyword is used.

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

  • If the Step keyword is used, stepcounter specifies the amount counter is incremented (if stepcounter is positive) or decremented (if it is negative).

Example

The following example demonstrates the use of a For...Next statement to iterate through the items in a combo box until an item in the combo box list matches a particular value entered in a text box:

 Dim sSought As String = txtSeek.Text Dim i As Integer Dim iCount As Integer = cboCombo.Items.Count For i = 0 To iCount - 1    If cboCombo.Items(i) = sSought Then       cboCombo.SelectedIndex = i       Exit For    End If Next i 

The following example demonstrates how to iterate from the end to the start of an array of values:

 For i = UBound(sArray) to LBound(sArray) Step - 1     Console.WriteLine(sArray(i)) Next i 

The following example demonstrates how to select only every other value from an array of values:

 For i = LBound(sArray) to UBound(sArray) Step 2     Console.WriteLine(sArray(i)) Next i 

Programming Tips and Gotchas

  • You can also nest For...Next loops , as shown here:

     For iDay = 1 to 365    For iHour = 1 to 23       For iMinute = 1 to 59             ...       Next iMinute    Next iHour Next iDay 
  • Although the counter following the Next keyword is optional, you will find your code is much easier to read if you use it, especially when nesting For... Next loops.

  • You can increment the loop by a non-integral value by supplying a Single, Double, or Decimal value in the Step clause. This also requires that counter be a Single, Double, or Decimal data type. If counter is a Single or Double, what should be the final iteration of the loop may be skipped because of rounding error. To prevent this half the value of stepcounter can be added to maximum_value . For example:

     Dim sngCtr As Single For sngCtr = 1 to 2.05 Step .1 
  • You should avoid changing the value of counter in the code within the loop. Not only can this lead to unexpected results; it makes for code that's incredibly difficult to read and to understand.

  • 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.

See Also

For Each...Next Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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