Bookshelf Home | Contents | Index | Search | PDF |
Siebel VB Language Reference > Siebel VB Language Reference >
For...Next Statement
This standard VB control structure repeats a series of program lines a fixed number of times.
Syntax
For counter = start To end [Step increment]
statement_block
[Exit For]
statement_block
Next [counter]
Returns
Not applicable
Usage
The start and end values must be consistent with increment. If end is greater than start, increment must be positive. If end is less than start, increment must be negative. Siebel VB compares the sign of (start - end) with the sign of increment. If the signs are the same, and end does not equal start, the For...Next loop is started. If not, the loop is omitted in its entirety.
With a For...Next loop, the program lines following the For statement are executed until the Next statement is encountered. At this point, the Step amount is added to the counter and compared with the final value, end. If the beginning and ending values are the same, the loop executes once, regardless of the Step value. Otherwise, the Step value controls the loop as follows:
Within the loop, the value of the counter should not be changed, as changing the counter makes programs more difficult to maintain and debug.
For...Next loops can be nested within one another. Each nested loop should be given a unique variable name as its counter. The Next statement for the inside loop must appear before the Next statement for the outside loop. The Exit For statement can be used as an alternative exit from For...Next loops.
If the variable is left out of a Next statement, the Next statement matches the most recent For statement. If a Next statement occurs prior to its corresponding For statement, Siebel VB returns an error message.
Multiple consecutive Next statements can be merged together. If this is done, the counters must appear with the innermost counter first and the outermost counter last. For example:
For i = 1 To 10
statement_block
For j = 1 To 5
statement_blockNext j, i
Example
For an example, read CSng Function.
See Also
Bookshelf Home | Contents | Index | Search | PDF |
Siebel VB Language Reference Published: 18 June 2003 |