Indefinite
Loop
Structures
ff
you
can't
specify
in
advance
how
many
times to
repeat
a loop, you
can
write
an
indefinite
loop
structure
that
contains
both
a
loop
clause
and
a test clause.
The
clauses
are
executed alternatingly,
with
the
re-
sult
of
the
test clause
determining
whether
to
continue.
This section describes two types
of
indefinite loop structure.
The
first,
DO
...
UNTIL
...
END, executes
the
loop clause before
the
test clause.
Consequently,
the
loop clause is always executed
at
least once.
The
second
type, WHILE
...
REPEAT
...
END, executes
the
test clause
first. Consequently, in
some
cases
the
loop clause is
never
executed.
DO
...
UNTIL
...
END
The
following example evaluates
an
object
repeatedly
until
evaluation
doesn't
change
the
object. Since
evaluation
must
occur
at
least once
before
the
first test
can
be
made,
this example uses
DO
...
UNTIL
...
END.
00
OUP
EVAL
UNTIL
OUP
ROT
SAME
END
This structure works as follows:
1.
The
loop clause 0 U PEl,} A L is executed, leaving
the
object
and
the
evaluated
result
on
the
stack.
2.
The
test clause
OUP
ROT
::;At'1E
is evaluated, leaving
the
evalu-
ated
result
and
a flag
on
the
stack.
The
flag
indicates
whether
the
object
and
the
evaluated
result
are
the
same.
3.
The
flag is
taken
from
the
stack. Its value
determines
whether
the
loop structure is
repeated
.
•
If
the
flag is false,
steps
(1), (2),
and
(3) are
repeated.
•
If
the
flag is true,
the
loop structure ends.
26:
Program
Structures
231