IBM InfoSphere DataStage, Version 11.3.1
FOR statement
Syntax
FOR variable = start TO end [STEP increment]
[loop.statements]
[CONTINUE | EXIT]
[ {WHILE | UNTIL} expression]
[loop.statements]
[CONTINUE | EXIT]
NEXT [variable]
Description
Use the FOR statement to create a FOR...NEXT program loop. A program loop is a series of statements that execute repeatedly until the specified number of repetitions have been performed or until specified conditions are met.
variable is assigned the value of start, which is the initial value of the counter. end is the end value of the counter.
The loop.statements that follow the FOR clause execute until the NEXT statement is encountered. Then the counter is adjusted by the amount specified by the STEP clause.
At this point a check is performed on the value of the counter. If it is less than or equal to end, program execution branches back to the statement following the FOR clause and the process repeats. If it is greater than end, execution continues with the statement following the NEXT statement.
The WHILE condition specifies that as long as the WHILE expression evaluates to true, the loop continues to execute. When the WHILE expression evaluates to false, the loop ends, and program execution continues with the statement following the NEXT statement. If a WHILE or UNTIL expression evaluates to the null value, the condition is false.
The UNTIL condition specifies that the loop continues to execute only as long as the UNTIL expression evaluates to false. When the UNTIL expression evaluates to true, the loop ends and program execution continues with the statement following the NEXT statement.
expression can also contain a conditional statement. As expression you can use any statement that takes a THEN or an ELSE clause, but without the THEN or ELSE clause. When the conditional statement would execute the ELSE clause, expression evaluates to false; when the conditional statement would execute the THEN clause, expression evaluates to true. The LOCKED clause is not supported in this context.
You can use multiple WHILE and UNTIL clauses in a FOR...NEXT loop.
Use the CONTINUE statement within FOR...NEXT to transfer control to the next iteration of the loop, from any point in the loop.
Use the EXIT statement within FOR...NEXT to terminate the loop from any point within the loop.
If STEP is not specified, increment is assumed to be 1. If increment is negative, the end value of the counter is less than the initial value. Each time the loop is processed, the counter is decreased by the amount specified in the STEP clause. Execution continues to loop until the counter is less than end.
The body of the loop is skipped if start is greater than end, and increment is not negative. If start, end, or increment evaluates to the null value, the FOR statement fails and the program terminates with a run-time error message.
Nested Loops
You can nest FOR...NEXT loops. That is, you can put a FOR...NEXT loop inside another FOR...NEXT loop. When loops are nested, each loop must have a unique variable name as its counter. The NEXT statement for the inside loop must appear before the NEXT statement for the outside loop.
If you omit the variables in the NEXT statement, the NEXT statement corresponds to the most recent FOR statement. If a NEXT statement is encountered without a previous FOR statement, an error occurs during compilation.
INFORMATION Flavor
In an INFORMATION flavor account the FOR variable is checked to see if it exceeds end before increment is added to it. That means that the value of the FOR variable does not exceed end at the termination of the loop. In IDEAL, PICK, IN2, and REALITY flavors the increment is made before the bound checking. In this case it is possible for variable to exceed end. Use the FOR.INCR.BEF option of the $OPTIONS statement to get IDEAL flavor behavior in an INFORMATION flavor account.
Examples
In the following example, the loop is executed 100 times or until control is transferred by one of the statements in the loop:
FOR VAR=1 TO 100
NEXT VAR
Here are more examples of FOR...NEXT loops:
- Source Code
- Program Output
- FOR X=1 TO 10 PRINT "X= ",X NEXT X
X= 1 X= 2 X= 3 X= 4 X= 5 X= 6 X= 7 X= 8 X= 9 X= 10- FOR TEST=1 TO 10 STEP 2 PRINT "TEST= ":TEST NEXT TEST
TEST= 1 TEST= 3 TEST= 5 TEST= 7 TEST= 9- FOR SUB=50 TO 20 STEP -10 PRINT 'VALUE IS ',SUB NEXT
VALUE IS 50 VALUE IS 40 VALUE IS 30 VALUE IS 20- FOR A=1 TO 4 FOR B=1 TO A PRINT "A:B= ",A:B NEXT B NEXT A
A:B= 11 A:B= 21 A:B= 22 A:B= 31 A:B= 32 A:B= 33 A:B= 41 A:B= 42 A:B= 43 A:B= 44- PRINT 'LOOP 1 :' SUM=0 FOR A=1 TO 10 UNTIL SUM>20 SUM=SUM+A*A PRINT "SUM= ",SUM NEXT
LOOP 1 : SUM= 1 SUM= 5 SUM= 14 SUM= 30- PRINT 'LOOP 2 :' * Y=15 Z=0 FOR X=1 TO 20 WHILE Z<Y Z=Z+X PRINT "Z= ",Z NEXT X
LOOP 2 : Z= 1 Z= 3 Z= 6 Z= 10 Z= 15
Last updated: 2015-03-09
PDF version of this information: