FOR
General Forms:
1. |
FOR index_variable = expression_1 TO expression_2 [STEP expression_3] ... NEXT index_variable or END FOR | |
2. |
FOR ( initializer ; condition ; post-expression ) ... END FOR |
FOR ... TO
The FOR ... TO statement is used in conjunction with the NEXT or END FOR statement to form repetitive loops. Each FOR ... TO statement must be paired with a NEXT or END FOR statement. Both the FOR and NEXT statements must use the same reference variable. The END FOR statement matches the current FOR.
When the FOR ... TO statement is first executed the reference variable is set to the value of the first expression. Program execution then continues until the corresponding NEXT or END FOR statement is executed. If no STEP parameter is added then the reference variable is incremented by one and is tested against the second expression, if its value is less than or equal to the second expression, the loop will then restart with the new value. The reference variable will continue to be incremented until it would be greater than the expression, then program execution continues with the statement following the NEXT or END FOR statement.
If the loop ends in an END FOR then the condition is checked on entry and the loop may be skipped entirely if the end condition is already reached. However if the loop ends in a NEXT then the condition is checked only at the end of the loop and the statements contained in the loop will always be executed at least once even if the value of the reference variable exceeds the second expression. END FOR is safer and is to be preferred over NEXT which is retained only for backward compatibility.
The optional STEP parameter can be incorporated to change the step with which the reference variable is incremented. Without a STEP clause the loop variable is incremented by 1 at the end of the loop. If a negative step is used then the reference variable will be decremented by the given step each time the loop is re-executed. If the STEP value is zero then, with a loop ending in END FOR, no loop will be performed while for a loop ending in NEXT, the code in the body of the FOR will be executed once.
After the loop is exhausted the loop variable will have the value used in the last iteration.
FOR ( initializer ; condition ; post-clause )
The FOR ( ... ) statement is used in conjunction with the END FOR statement to form repetitive loops. Each FOR ( ... ) statement must be paired with an END FOR statement.
When the FOR ( ... ) statement is first executed, the initializer statement is executed, and the condition is tested. If the condition is true, program execution continues until the corresponding END FOR statement; otherwise the loop is skipped. When the corresponding END FOR statement is executed, the post-clause statement is executed and the condition is tested again, with the loop and post-clause being continually executed until the condition is false.
If a BREAK statement is encountered in the middle of the loop, the loop is jumped out of. If a CONTINUE statement is encountered in the middle of a loop, the remainder of the loop is skipped, the post-clause is executed, and the condition is evaluated again for another possible loop iteration.
Examples:
FOR Count1 = 10 TO 1 STEP -1
FOR Count2 = 1 to 100
CONVERT INT(RND(1)*1000) TO Abc$,(####)
.KCMLGrid1.MoveCell(Count2, Count1)
.KCMLGrid1.Cell.Text$ = Abc$
NEXT Count2
NEXT Count1
FOR ( Count1 = 10 ; Count1 >= 1 ; Count1-- )
FOR ( Count2 = 1 ; Count2 <= 100 ; Count2++ )
CONVERT INT(RND(1)*1000) TO Abc$,(####)
.KCMLGrid1.MoveCell(Count2, Count1)
.KCMLGrid1.Cell.Text$ = Abc$
END FOR
END FOR
Jumping into a FOR ... TO loop will cause an error at the corresponding NEXT statement if the loop has not already been executed at least once.
Jumping out of a FOR ... TO loop is allowed but should be avoided as it is bad programming practice. Terminating loops incorrectly can cause stack overflow errors. If a program has to jump out of a loop then a BREAK statement should be used to abandon the loop entirely or the CONTINUE statement may be used skip the remaining body of the loop. E.g.
FOR Count = pos1 TO pos2
CONVERT Count to Text$,(####)
.static1.Text$ = Text$
IF count>50 AND count<80 THEN CONTINUE
IF count>100 THEN BREAK
'Update_Record()
END FOR
In the above example the subroutine 'update_record would only be executed if the variable count is less than 50 or greater than 80, if the variable count is greater than 100 then the loop is abandoned, leaving the variable count unchanged.
FOR loops are automatically indented by the KCML editor provided that when the program is resolved each FOR ... TO statement has a corresponding NEXT or END FOR.
Compatibility:
END FOR was introduced with KCML 6.60. The use of NEXT is now deprecated. The use of a single NEXT with more than one index_variable is strongly deprecated and may be withdrawn at any time.
The FOR ( a ; b ; c ) construct was introduced with KCML 7.05.
Syntax examples:
FOR Loop = 1 TO last_record
FOR Count = last TO first STEP -5
FOR Loop2 = first_5 TO last_5 STEP 0.5
FOR ( i = 0 ; i < 10 ; i++ )
FOR ( bit = 1 ; bit <= 1024 ; bit *= 2 )
See also: