Reference Guide
3-90 Full Command and Function Reference
FOR
Type: Command Operation
Description: FOR Definite Loop Structure Command: Starts FOR … NEXT and FOR … STEP definite loop
structures.
Definite loop structures execute a command or sequence of commands a specified number of
times.
•
A FOR … NEXT loop executes a program segment a specified number of times using a local
variable as the loop counter. You can use this variable within the loop. The RPL syntax is this:
x
start
x
finish
FOR counter loop-clause NEXT
The algebraic syntax is this:
FOR(counter, x
start
, x
finish
) loop-clause NEXT
FOR takes x
start
and x
finish
as the beginning and ending values for the loop counter, then creates
the local variable counter as a loop counter. Then, the loop clause is executed; counter can be
referenced or have its value changed within the loop clause. NEXT increments counter by one,
and then tests whether counter is less than or equal to x
finish
. If so, the loop clause is repeated
(with the new value of counter).
When the loop is exited, counter is purged.
•
FOR … STEP works just like FOR … NEXT, except that it lets you specify an increment
value other than 1. The syntax RPL is:
x
start
x
finish
FOR counter loop-clause
x
increment
STEP
The algebraic syntax is:
FOR(counter, x
start
, x
finish
) loop-clause, STEP (x
increment
)
FOR takes x
start
and x
finish
as the beginning and ending values for the loop counter, then creates
the local variable counter as a loop counter. Next, the loop clause is executed; counter can be
referenced or have its value changed within the loop clause. STEP takes x
increment
and increments
counter by that value. If the argument of STEP is an algebraic expression or a name, it is
automatically evaluated to a number.
The increment value can be positive or negative. If the increment is positive, the loop is
executed again when counter is less than or equal to x
finish
. If the increment is negative, the loop
is executed when counter is greater than or equal to x
finish
.
When the loop is exited, counter is purged.
Access: !°
BRANCH FOR
( °is the left-shift of the Nkey).
Input/Output:
Level 2/ Level 1 Level 1/Item 1
FOR x
start
x
finish
→
NEXT
→
FOR x
start
x
finish
→
STEP
x
increment
→
STEP
'symb
increment
'
→
Note: It should be noted that FOR inputs may also be integers (object type 28) and binary integers (type
10). FOR actually runs fastest on binary integers, runs “normally” on reals and slightly slower on
integers.
Example: The following program sums all odd integers in the range 1 to 100:
« 0 1 100 FOR I I + 2 STEP »
See also: NEXT, START, STEP