Example: The following program takes n from the stack, and returns the series of numbers 1, 2, 4, 8, 16, …, n. If n isn’t in the series, the program stops at the last value less than n.

«1 SWAP FOR n n n STEP »

The first n is the local variable declaration for the FOR loop. The second n is put on the stack each iteration of the loop. The third n is used by STEP as the step increment.

Using Indefinite Loop Structures

The DO 7 UNTIL 7 END Structure

The syntax for this structure is

«DO loop-clause UNTIL test-clause END »

DO… UNTIL… END executes the loop-clausesequence repeatedly until test-clausereturns a true (nonzero) result. Because the test-clause is executed after the loop-clause, the loop-clause is always executed at least once.

Syntax

Flowchart

 

DO

 

 

loop-clause

Body of loop

 

UNTIL

TEST

 

test-clause

1: test result

 

 

Is test

no

END

result non-zero?

 

yes

DO 7 UNTIL 7 END Structure

DO starts execution of the loop-clause. UNTIL marks the end of the loop-clause. The test-clause leaves a test result on the stack. END removes the test result from the stack. If its value is zero, the loop-clause is executed again

otherwise, execution resumes following END. If the argument of END is an algebraic or a name, it’s automatically evaluated to a number.

To enter DO 7 UNTIL 7 END in a program:

Press !°%BRCH% !%DO%.

1-22 RPL Programming