Reference Guide

1-24 RPL Programming
The WHILE  REPEAT  END Structure
The syntax for this structure is
«
WHILE
test-clause
REPEAT
loop-clause
END
»
WHILE … REPEAT … END repeatedly evaluates test-clause and executes the loop-clause sequence 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
REPEAT
END
TEST
Is test
result non-zero?
no
yes
1: test result
loop-clause
WHILE
test-clause
Body of loop
WHILE  REPEAT  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  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
»