The WHILE REPEAT END Structure

The syntax for this structure is

« WHILE testclause REPEAT loopclause END »

WHILE … REPEAT … END repeatedly evaluates testclause and executes the loopclause Because the testclause is executed before the loopclause, the loopclause is not executed i

sequence if the test is true. f the test is initially false.

Syntax

WHILE

test-clause

REPEA T

loop-clause

END

Flowchart

TEST

1: test result

Is test

result non-zero?

no

yes

Body of loop

WHILE REPEAT END Structure

WHILE starts execution of the testclause, which re turns a test result to the stack. REPEAT takes the value from the stack. If the value is nonzero, execution continues with the loopclauseotherwise, execution resu mes following END. If the argument of REPEAT is an algebraic or a name, it’s automatically evaluated to a number.

To enter WHILE REPEAT END in a program:

Press !°%BRCH% ! %WHILE%.

Example: The following program starts with a number on the stack, and repeatedly performs a division by 2 as long as the result is evenly divisible. For example, starting with the number 24, the program computes 12, then 6, then 3.

«WHILE DUP 2 MOD 0 == REPEAT 2 / DUP END DROP »

Example: The following program takes any number of vectors or arrays from the stack and adds them to the statistics matrix. (The vectors and arrays must have the same number of columns.)

WHILE … REPEAT … END is used instead of DO … UNTIL … END because the test must be done before the addition. (If only vectors or arrays with the same number of columns are on the stack, the program errors after the last vector or array is added to the statistics matrix.)

«WHILE DUP TYPE 3 == REPEAT Σ+ END »

124 RPL Programming

Page 48
Image 48
HP 50g Graphing manual While Repeat END Structure, « … While testclause Repeat loopclause END … », Press !%BRCH% ! %WHILE%