Indefinite Loop Structures
The first kind of loop structure we'll see is the Indefinite Loop.
As the name says, it repeats an indefinite number of times, while or
until a condition is met. There are two forms of indefinite loops in
the HP48. WHILE...REPEAT...END
loops and DO...UNTIL...END
loops. Basically, they do the same thing: repeat the code while a
certain condition is met, or until a condition is met. The form of
the WHILE
structure is:
WHILE
test condition
REPEAT
code to be repeated
END
When te HP sees WHILE
in a program, it evaluates
the test condition
. If it is true, then the
code to be executed
is executed, and when END
is reached, the program re-starts the process at WHILE
.
If the test condition
evaluates false, then
the program resumes after the END
. Since the condition
is evaluated before the main code, it may never be executed, if the
condition first evaluates false.
Let's see a simple example of WHILE
. The program
calculates the sum of the even numbers between 100 and 200.
<< 0 @ Initial sum
1 @ First number
-> sum n @ We could manipulate the stack, but let's
@ use local variables to make things easier
<< WHILE
n 200 <
REPEAT @ Repeating while the sum hasn't reached 200
n 'sum' STO+ @ Increment sum
'n' 2 STO+ @ Increment count
@ Oh, yes, STO+ accepts its arguments in any order
END
sum @ Output the sum
>>
>>
The other form of indefinite loop is DO
. It's
behavour is like WHILE
. But instead of repeating
while the condition is true, it repeats until the
condition is true. Or, while it's false. The form of it is:
DO
code to be executed
UNTIL
test condition
END
The code to be executed
will be executed
at least once, until test condition
evaluates
true. A note for both WHILE
and DO
: if
you use something like
WHILE 1 REPEAT ... END
the program will be executed until your
batteries have completely drained or the program is stopped. So
be careful that sometime the condition must stop the loop.
Now, let's see the above program, rewritten for DO
.
Only the essential has changed, the rest remained the same.
<< 0 100 -> sum n
<< DO
'sum' n STO+
2 'n' STO+
UNTIL
n 200 >
END
sum
>>
>>
Definite Loop Structures
Opposing to the structures above, which will run forever if
you let them, the Definite Loop Structures run a
determined number of times, no more and no less. There are
two definite loop structures on the HP48, and they are very
similar, differing only in a detail.
The first one is START...NEXT
. It's form is:
start stop START
commands to be executed
NEXT
START
reads two numbers from the stack, the
initial number and the final number. Then, the commands
to e executed
are executed. When NEXT
is
reached, the initial nubmer is incremeted by one. If it is
smaller or equal to the final number, then the commands will be
executed again. If it is greater, then execution will continue
after NEXT
.
And, finally, the second definite loop structure is
FOR...NEXT
. It is exactly like START
,
with one difference: you can acess the number of that execution
and use it. The sintaxis is:
start stop FOR var
commands to be executed
NEXT
The variable can have any name, and works like any local
variable. Traditionally, the variable is called i
,
j
or k
. But this is only a suggestion.
In START
and FOR
structures, you can
change NEXT
by
increment STEP
The difference is that the count will
be increment by increment
instead of 1.
Now, let's see the above program, in it's most efficient (well,
amost) form:
<< 0 @ Initial sum
100 200 @ Goes from 100 to 200
FOR i @ The current number is i
i + @ Increment sum by i
2 STEP @ Increment by 2, since we want only even numbers
>>