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 UNTIL END Structure

The syntax for this structure is

«DO loopclause UNTIL testclause END »

DO… UNTIL… END executes the loopclause sequence repeatedly until testclause returns a true (nonzero) result. Because the testclause is executed after the loopclause, the loopclause is always execute d 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 UNTIL END Structure

DO starts execution of the loopclause. UNTIL mark s the end of the loopclause. The testclause leave s a test result on the stack. END removes the test result from the stack. If its value is zero, the loopclause 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 UNTIL END in a program:

Press !°%BRCH% ! %DO%.

122 RPL Programming

Page 46
Image 46
HP 50g Graphing manual Using Indefinite Loop Structures, Do Until END Structure, « … do loopclause Until testclause END … »