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:

xstart xfinish FOR counter loop-clause NEXT The algebraic syntax is this:

FOR(counter, xstart, xfinish) loop-clause NEXT

FOR takes xstart and xfinish 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 xfinish. 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:

xstart xfinish FOR counter loop-clause xincrement STEP The algebraic syntax is:

FOR(counter, xstart, xfinish) loop-clause, STEP (xincrement)

FOR takes xstart and xfinish 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 xincrement 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 xfinish. If the increment is negative, the loop is executed when counter is greater than or equal to xfinish.

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 xstart

xfinish

 

 

 

NEXT

 

 

 

 

FOR xstart

xfinish

 

 

 

STEP

xincrement

 

 

 

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

3-90 Full Command and Function Reference