Tuesday, October 18, 2011

RPL Programming Tutorial - Part 10 - HP 49g+/50g: The DO Loop

Do It Until It's Done!

Welcome to the third week of my RPL tutorials for the HP 49g+ and 50g Hewlett Packard graphing calculators. We will start this batch of tutorials with Part 10: the Do Loop.

The DO-UNTIL-END loop structure works like the WHILE-REPEAT-END loop structure except instead of repeating a bunch of designated commands while a certain condition is met, this time we are repeating a bunch of designated commands until a condition is met.

A DO-UNTIL-END structure looks like this:

DO commands
UNTIL this condition is met
END


Key sequence from the PRG-BRCH menu:

[LS] [F5] (DO)

Let's illustrate a use of a DO-UNTIL-END structure with a simple program: have the calculator generate a bunch of random numbers until the sum of 1 is reached.

It's a new week - so here is a reminder of how I label shift keys:

[RS] is the right shift key, 2nd key up from the ON button on the left side of the keyboard. [RS] is red on the 49g+ and orange on the 50g.

[LS] is the left shift key, 3rd key up from the ON button on the left side of the keyboard. [LS] is green on the 49g+ and white on the 50g.

[ALPHA] is the 4th key up from the ON button on the left side of the keyboard. On both the 49g+ and the 50g, the key is yellow. Pressing [ALPHA] twice puts the calculator in ALPHA-LOCK mode.

[big X] is the "X" key on the 4th row from the bottom, 3rd from the left. This is so I distinguish X from the times key ([ x ]).

Finally, the soft keys labeled F1 - F6 are on the top row.

Add Random Numbers until I get at least 1

This program will keep adding random numbers until the sum exceeds one.

The Program RAND1

Comments are italicized beginning with an asterisk. Here we go!

[RS] [ + ]
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND)

* Inserts the first random number. RAND always enters numbers between 0 and 1.
[LS] [EVAL] (PRG) [F3] (BRCH)
[LS] [F5] (DO)

* Inserts the DO-UNTIL-END structure
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND)
[ + ] [ ↓ ]

* Enter the commands that are to be repeated (RAND + )
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
1 [LS] [1/X] ≥

* Enters the condition (sum≥1)
[ENTER]
* Terminates program entry

[ ' ] [ALPHA] [ALPHA] [ √ ] (R) [F1] (A) [EVAL] (N) [F4] (D) [ENTER] [STO>]

The completed program:

<< RAND
DO RAND +
UNTIL DUP 1 ≥
END >>


Instructions:

Just run RAND1.

Examples of some results you might get: 1.46211570675, 1.12775622211, 1.50610262525

That wraps up Part 10 of the RPL Tutorial Series. Next time, we'll look at the CASE structure. See you then!

Eddie

This tutorial is property of Edward Shore. Mass distribution and reproduction requires express permission of the author.

Fun with the HP 30b

Fun with the HP 30 b Introduction The following programs are for the HP 30b Business Professional. Did you know that the 30b...