Using Loop Counters - HP 48gII Advanced User's Reference Manual

Graphing calculator
Hide thumbs Also See for 48gII:
Table of Contents

Advertisement

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 »

Using Loop Counters

For certain problems you may need a counter inside a loop structure to keep track of the number of loops. (This
counter isn't related to the counter variable in a FOR ... NEXT/STEP structure.) You can use any global or local
variable as a counter. You can use the INCR or DECR command to increment or decrement the counter value
and put its new value on the stack.
The syntax for INCR and DECR is
« ... 'variable' INCR ... »
or
« ... 'variable' DECR ... »
To enter INCR or DECR in a program:
Press !° #MEM# %ARITH% %INCR% or %DECR%.
!
The INCR and DECR commands take a global or local variable name (with ' delimiters) as their argument
— the variable must contain a real number. The command does the following:
1. Changes the value stored in the variable by +1 or -1.
2. Returns the new value to the stack.
If c contains the value 5, then 'c' INCR stores 6 in c and returns 6 to the stack.
Examples:
The following program takes a maximum of five vectors from the stack and adds them to the current statistics
matrix.
Program:
«
0  c
«
WHILE
DUP TYPE 3 ==
'c' INCR
5 ‰
AND
REPEAT
...+
END
»
»
1-24 RPL Programming
Comments:
Stores 0 in local variable c.
Starts the defining procedure.
Starts the test clause.
Returns true if level 1 contains a vector.
Increments and returns the value in c.
Returns true if the counter c 5.
Returns true if the two previous test results are true.
Adds the vector to DAT.
Ends the structure.
Ends the defining procedure.

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

49g+

Table of Contents