Tuesday, October 11, 2011

RPL Programming Tutorial - Part 8 - HP 49g+/50g: The WHILE Loop

While We Are Here...

Welcome to Part 8 of the RPL Programming Tutorial. Today, we will cover the WHILE-REPEAT-END programming structure. This structure is similar to the WHILE-WEND programming structure in BASIC. In this loop certain commands are repeated while a condition is true.

The WHILE-REPEAT-END structure looks like this:

WHILE this condition is met
REPEAT these instructions

The Program DBY2

This program takes the absolute number from the stack. As long as the number is greater than 1, the program keeps dividing the number by 2.

Note the use of DUP in this program. The DUP (Duplicate) command duplicates the contents of Level 1. It is necessary because tests consume the contents of levels 2 and 1.

[RS] [ + ] (<< >>)
[LS] [ ÷ ]
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)

* The DUP command
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F6] (WHILE)
* Enters the WHILE-REPEAT-END structure
1 [LS] [1/X] ≥
* Enters the while condition
[ &darr ] 2 [ ÷ ] [LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
* Enters the loop instructions
* Simplifies the number and terminates program entry

[ ' ] [ALPHA] [ALPHA] [F4] (D) [F2] (B) [1/X] (Y) 2 [ENTER] [STO>]

The Complete Program:



1. Enter a number.
2. Run DBY2.


10 returns 5/8
96 returns 3/4
-105 returns 105/128

This concludes Part 8 of our tutorial. Next time, we will create custom menus. It's going to be a lot of fun! Until next time, Eddie.

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

No comments:

Post a Comment

Swiss Micros DM16L: Advanced Boolean and Factorial (up to 20)

Swiss Micros DM16L:   Advanced Boolean and Factorial (up to 20) Introduction The program listing, for the Swiss Micros DM16L and Hewlett Pac...