Using Loop Counters

For certain problems you may need a counter inside a loop structure to keep track of the number of loops. (This counter isn’t related to the counter variable in a FOR … NEXT/STEP structure.) You can use any global or local variable as a counter. You can use the INCR or DECR command to increment or decrement the counter value and put its new value on the stack.

The syntax for INCR and DECR is

«'variable' INCR »

or

«'variable' DECR »

To enter INCR or DECR in a program:

Press !°#MEM# %ARITH% %INCR% or %DECR%.

The INCR and DECR commands take a global or local variable name (with ' delimiters) as their argument — the variable must contain a real number. The command does the following:

1.Changes the value stored in the variable by +1 or -1.

2.Returns the new value to the stack.

Example: If c contains the value 5, then 'c' INCR stores 6 in c and returns 6 to the stack.

Example: The following program takes a maximum of five vectors from the stack and adds them to the current statistics matrix.

Program:

Comments:

 

 

«

 

0 → c

Stores 0 in local variable c.

«

Starts the defining procedure.

WHILE

Starts the test clause.

DUP TYPE 3 ==

Returns true if level 1 contains a vector.

'c' INCR

Increments and returns the value in c.

5 ‰

Returns true if the counter c≤5.

AND

Returns true if the two previous test

 

results are true.

REPEAT

Adds the vector to ΣDAT.

Σ+

 

END

Ends the structure.

»

Ends the defining procedure.

»

 

 

 

RPL Programming 1-25