Caché ObjectScript Reference
FOR (legacy version)
[Back] [Next]
   
Server:docs1
Instance:LATEST
User:UnknownUser
 
-
Go to:
Search:    

Executes a command loop repeatedly, testing at the beginning of each loop.
Synopsis
FOR variable=forparameter,... command
F variable=forparameter,... command
where forparameter can be:
expr
start:increment
start:increment:end
and command is one or more Caché ObjectScript commands on the same program line. This command (or commands) are executed repeatedly, based on the FOR test.
Arguments
variable Optional — A counter variable for the FOR loop.
expr Optional — The value of variable before executing the loop commands.
start Optional — The value of variable on the first iteration of the FOR loop.
increment Optional — The value used to increment variable after each iteration of the FOR loop.
end Optional — The value used to terminate the FOR loop.
Description
Note:
This page describes the legacy version of the FOR command. This version is considered legacy as of Caché 4.0, and should not be used in new programming. It is described here solely for compatibility with legacy applications.
The legacy FOR command is line-oriented; the loop it executes consists of commands that follow it on the same program line. Curly braces are not used and line formatting is restrictive. The new FOR command is block structured; the loop it executes consists of commands found within the curly braces that follow the FOR command. Line formatting (white space, line breaks) is unrestrictive.
The FOR command has two basic forms:
FOR Without an Argument
FOR without an argument executes the command(s) that follow it on the same line indefinitely. Caché repeats these commands until it encounters a QUIT or GOTO command.
The FOR keyword without an argument must be separated from the command that follows it by at least two blank spaces.
FOR With an Argument
The action FOR performs depends on the argument form you use.
FOR variable=expr executes the commands that follow it on the same line once, setting variable to the value of expr.
FOR variable=start:increment executes the commands that follow it on the same line indefinitely. On the first iteration, Caché sets variable to the value of start. Each execution of the FOR command increments the variable value by the specified increment value. Caché repeats the commands until it encounters a QUIT or GOTO command.
FOR variable=start:increment:end sets variable to the value of start. Caché then executes the commands that follow it on the same line based on the conditions described in this table:
increment is positive increment is negative
If start>end, do not execute FOR. When variable > (end-increment), or if Caché encounters a QUIT or GOTO command, stop. If start<end, do not execute FOR. When variable<(end-increment), or if Caché encounters a QUIT or GOTO command, stop.
Caché evaluates the start, increment, and end values when it begins command execution. Any changes made to these values within the loop are ignored and have no effect on the number of loop executions.
When the loop terminates, variable contains a value that reflects the increment resulting from the last execution of the loop. However, variable is never incremented beyond the maximum value specified in end.
Arguments
variable
A variable that holds the current count for the FOR loop. It is a local variable name which holds the numeric value specified by the other arguments of the FOR command.
expr
The numeric value Caché assigns to variable before executing the loop commands. The value of expr can be specified as a literal or any valid expression; Caché evaluates expr for its numeric value.
start
The numeric value Caché assigns to variable on the first iteration of the FOR loop. The value of start can be specified as a literal or any valid expression; Caché evaluates start for its numeric value.
increment
The numeric value Caché uses to increment variable after each iteration of the FOR loop. It is required if you specify start. The value of increment can be specified as a literal or any valid expression; Caché evaluates increment for its numeric value.
end
The numeric value Caché uses to terminate a FOR loop. When variable equals this value, the FOR loop is executed one last time and then terminated. The value of end can be specified as a literal or any valid expression; Caché evaluates end for its numeric value.
Examples
Argumentless FOR
In the following example, demonstrating argumentless FOR, the user is prompted repeatedly for a number that is then passed to the Calc subroutine by the DO command. The FOR loop terminates when the user enters a null string (presses ENTER without inputting a number), which causes the QUIT command to execute. The argumentless FOR keyword must be followed by two (or more) spaces.
FOR  READ !,"Number: ",num QUIT:num=""  DO Calc(num)
Using FOR variable=expr
When you specify variable=expr, Caché executes the FOR command(s) once. The value in expr can be a literal or any valid expression. If you specify an expression, it must evaluate to a single numeric value.
In the following example, the WRITE command on the line with the FOR command executes once, with num having the value 4. It writes the number 12:
  SET val=4
  FOR num=val WRITE num*3 QUIT
Using FOR variable=start:increment:end
The arguments start, increment, and end specify a start, increment, and end value, respectively. All three are evaluated as numbers. They can be integer or real, positive or negative. If you supply string values, they are converted to their numeric equivalents at the start of the loop.
When Caché first enters the loop, it assigns the start value to variable and compares the variable value to the end value. If the variable value is less than the end value (or greater than it, in the case of a negative increment value), Caché executes the loop commands. It then updates the variable value using the increment value. (The variable value is decremented if a negative increment is used.)
Execution of the loop continues until the incrementing of the variable value would exceed (not just equal) the end value (or until Caché encounters a QUIT or GOTO). At that point, to prevent variable from exceeding end, Caché suppresses variable assignment and loop execution ends. If the increment causes the variable value to equal the end value, Caché executes the FOR loop one last time and then terminates the loop.
The following code executes the WRITE command repetitively to output, in sequence, all of the characters in string1, except for the last one. Even though the end value is specified as len-2, only the last character will be skipped. This is because the loop is terminated only when the variable value exceeds (not just matches) the end value.
  SET len=$LENGTH(string1)
  FOR index=1:1:len-2 WRITE $EXTRACT(string1,index)
Using FOR variable=start:increment
In this form of the FOR command there is no end value; the commands following the FOR must contain a QUIT or GOTO command to terminate the loop. In many cases, the FOR invokes a DO loop, which contains the QUIT or GOTO command.
The start and increment values are evaluated as numbers. They can be integer or real, positive or negative. If string values are supplied, they are converted to their numeric equivalents at the start of the loop. Caché evaluates the start and increment values when it begins execution of the loop. Any changes made to these values within the loop are ignored.
When Caché first enters the loop, it assigns the start value to variable and executes the loop commands. It then updates the variable value using the increment value. (The variable value is decremented if a negative increment is used.) Execution of the loop continues until Caché encounters a QUIT or GOTO within the loop.
The following example illustrates use of the FOR variable=start:increment form to compute an average for a series of user-supplied numbers. The postconditional QUIT is included to terminate execution of the loop when the user enters a null string (that is, presses ENTER without inputting a value). When the postconditional expression (num="") tests TRUE, Caché executes the QUIT and terminates the DO loop.
The loop counter (the i variable) is used to keep track of how many numbers have been entered. i is initialized to 0 because the counter increment occurs after the user inputs a number. Caché terminates the loop when the user enters a null. After the loop is terminated, the SET command references i (as a local variable) to calculate the average.
  SET sum=0
  FOR i=0:1 DO Averageloop
  SET average=sum/i
Averageloop
    READ !,"Number: ",num 
    QUIT:num="" 
    SET sum=sum+num
Further Examples of FOR with Increment Syntax
The following example of the FOR variable=start:increment form prompts the user for a number and then passes the number and the current value of x to the Calc subroutine invoked by the DO command. The value of x is initialized to 1 and is incremented by 1 for each execution of the loop. The FOR loop terminates when the user presses ENTER, which causes the QUIT command to execute.
  FOR x=1:1 READ !,"Number: ",num QUIT:num="" DO Calc(num,x)
Calc(a,b)
The following example of the FOR variable=start:increment:end form prompts the user for a number and then passes the number to the Calc subroutine invoked by DO. The value of x is initialized to 1 and is incremented by 1 for each execution of the loop. The FOR loop terminates either when the value of x would exceed the end value (3) or when the user presses ENTER, causing the QUIT command to execute.
Mainloop
  FOR x=1:1:3 READ !,"Number: ",num QUIT:num="" DO Calc(num)
Calc(a)
The following example of the FOR variable=start:increment:end form shows a FOR loop where the WRITE command is never executed, since the first value of i (10) is already greater than the end value (1) minus the increment value (1).
   FOR i=10:1:1 WRITE i
The following example shows a FOR loop that executes the WRITE command 10 times then completes with i=10.
   FOR i=1:1:10 WRITE i
The following example shows a FOR loop that executes the WRITE command 10 times then completes with i=11.
  SET i=1 
  FOR i=1:0:10 WRITE i,! SET i=i+1
Using FOR with Multiple forparameters
A single FOR command can contain both types of parameter syntax. The following is an example of FOR with multiple forparameters. The first forparameter is the expr syntax. The second forparameter is the start:increment:end syntax. The two forparameters are separated by a comma. The first time through the FOR, Caché uses the expr syntax, and invokes the Test subroutine with x equal to the value of y. In the second (and subsequent) iterations, Caché uses the start:increment:end syntax. It sets x to 1, then 2, etc. On the final iteration, x=10.
Mainloop
  FOR x=y,1:1:10 DO Test
Test
Incrementing with Argumentless FOR
The argumentless FOR operates the same as the FOR variable=start:increment form. The only difference is that it does not provide a way to keep track of the number of loop executions. Two (or more) spaces are required after the FOR keyword in an argumentless FOR command.
The following example shows how the previous loop counter example might be rewritten using the argumentless FOR. The assignment i=i+1 replaces the loop counter. Note the two spaces following the argumentless FOR keyword.
Average2loop
  SET sum=0
  SET i=0
  FOR  READ !,"Number: ",num QUIT:num="" SET sum=sum+num,i=i+1
  SET average=sum/i
  WRITE !!,"Average is: ",average
  QUIT
Notes
FOR and Watchpoints
You have limited use of watchpoints with FOR. If you establish a watchpoint for the control (index) variable of a FOR command, Caché triggers the specific watchpoint action only on the initial evaluation of each FOR command argument. This restriction is motivated by performance considerations.
The following example contains three kinds of FOR command arguments for the watched variable X: a range, with initial value, increment, and limit (final value); a single value; and a range with initial value, increment, and no limit. Breaks occur when X has the initial values 1, 20, and 50.
USER>ZBREAK *X
USER>FOR X=1:1:10,20,50:2 SET T=X QUIT:X>69
<BREAK>
USER 2f0>w
X=1
USER 2f0>g
USER>FOR X=1:1:10,20,50:2 SET T=X QUIT:X>69
<BREAK>
USER 2f0>w
T=10
X=20
USER> 2f0>g
USER>FOR X=1:1:10,20,50:2 SET T=X QUIT:X>69
<BREAK>
USER 2f0>w
T=20
X=50
USER 2f0>g
USER>w
T=70
X=70
Terminating a FOR Loop with QUIT or GOTO
A FOR loop is terminated by a QUIT only if the QUIT appears as one of the loop commands. If Caché encounters a QUIT in a subroutine called by the DO command, it terminates only the subroutine, not the FOR loop itself.
A FOR loop is terminated by a GOTO that appears anywhere within the loop. If a GOTO exits from a subroutine called by the DO command, Caché terminates both the subroutine and the FOR loop.
See Also