The WHILE 7 REPEAT 7 END Structure

The syntax for this structure is

«WHILE test-clause REPEAT loop-clause END »

WHILE … REPEAT … END repeatedly evaluates test-clauseand executes the loop-clausesequence if the test is true. Because the test-clause is executed before the loop-clause, the loop-clause is not executed if the test is initially false.

Syntax

Flowchart

WHILE

TEST

test-clause

1: test result

REPEA T

Is test

result non-zero?

no

yes

loop-clause

Body of loop

 

 

 

 

 

 

END

WHILE 7 REPEAT 7 END Structure

WHILE starts execution of the test-clause, which returns a test result to the stack. REPEAT takes the value from the stack. If the value is nonzero, execution continues with the loop-clause-otherwise, execution resumes following END. If the argument of REPEAT is an algebraic or a name, it’s automatically evaluated to a number.

To enter WHILE 7 REPEAT 7 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 »

1-24 RPL Programming