Siebel VB Language Reference > Methods Reference for Siebel VB > Code Control Statements >

For Next Statement


The For Next statement is a control structure that repeats a series of code lines a fixed number of times. It does not return a value.

Format

For counter = start To end [Step increment]

     statement_block

     [Exit For]

     statement_block

Next [counter]

The following table describes the arguments that you can use with this method.

Argument
Description

counter

A numeric variable for the loop counter.

start

The initial value of the counter.

end

The ending value of the counter.

increment

The amount by which Siebel VB modifies the counter each time the loop runs. The default value is 1.

statement_block

The Visual Basic functions, statements, or methods to run.

How Siebel VB Handles a For Next Statement

Siebel VB compares the sign of start and the sign of end to the sign of increment, and then does one of the following:

  • If the signs are the same, and if end does not equal start, then it starts the For Next loop.
  • If the signs are not the same, then it skips the loop entirely.

Siebel VB does the following when it runs a For Next loop:

  1. Runs the code lines that occur after the For statement until it encounters the Next statement.
  2. Adds the Step amount to the counter.
  3. Compares the value in the counter argument to the value in the end argument.
  4. Does one of the following:
    • If the beginning and ending values are the same. It runs the loop one time, regardless of the Step value.
    • If the beginning and ending values are not same. It uses the Step value to control the loop as follows:
      Step Value
      Description

      Positive

      Siebel VB does one of the following, depending on the value of the counter:

      • The counter is less than or equal to the value in the end argument. It adds the Step value to counter. Control returns to the statement after the For statement and the process repeats.
      • The counter is greater than the value in the end argument. It exits the loop, and then resumes running code with the statement that occurs immediately after the Next statement.

      Negative

      Siebel VB repeats the loop until the counter is less than the value in the end argument.

      Zero

      Siebel VB repeats the loop indefinitely.

Usage

The values in the start argument and the end argument must be consistent with the value in the increment argument:

  • If end is greater than start, then increment must be positive.
  • If end is less than start, then increment must be negative.

You must not modify the value of the counter while the loop runs. Changing this value makes the code more difficult to maintain and debug.

You can use the Exit For statement as an alternative exit from a For Next loop.

Nesting a For Next Loop

You can nest a For Next loop in another For Next loop:

  • You must specify a unique variable name for the counter in each nested loop.
  • You must make sure the Next statement of the inner loop occurs before the Next statement of the outer loop.

To merge Next statements that are multiple and consecutive, you must make sure the innermost counter occurs first and the outermost counter occurs last. For example:

For i = 1 To 10
   
statement_block
   For j = 1 To 5
      statement_block
Next j, i

If you do not include the variable in a Next statement, then this Next statement matches the most recent For statement. If a Next statement occurs prior to the corresponding For statement for this Next statement, then Siebel VB returns an error message.

Example

For an example, see Convert Expression to Single-Precision Method.

Siebel VB Language Reference Copyright © 2015, Oracle and/or its affiliates. All rights reserved. Legal Notices.