If Only...
Welcome to Part 3 of the RPL Programming Tutorial with the HP 49g+ and HP 50g calculator. To recap, Part 1 discussed the basics of RPL programming and Part 2 introduced the concept of local variables.
In Part 3 we will dive into the IF-THEN and IF-THEN-ELSE structures. These structures test data against a condition, and then instructs the machine to execute designated program code based on the results.
In general, an IF-THEN-ELSE-END structure looks like this:
IF condition listed here (i.e. x = 5, y > 10, z ≤ 0, a ≠ b, etc.)
THEN commands to be done if the condition is true
ELSE commands to be done if the condition is false
END
How to find the IF menu
You can input the IF, THEN, ELSE, and END program commands separately. However, the HP 50g (and 49g+) gives the user another choice. With a keystroke sequence, you can enter the entire sequence as a template.
To insert the templates:
Start by pressing [LS] [EVAL] (PRG) [F3] (BRCH) .
To insert an IF-THEN-END structure, press [LS] [F1] (IF).
To insert an IF-THEN-ELSE-END structure, press [RS] [F1] (IF).
The next two programs will show an example of each of the two structures.
The Online Shipping Deal
A famous online store is running a promotion: Order at least $100.00 from our store, and we'll pay the $5.95 shipping fee! This program calculates the total amount of the order, with shipping if any. Assume the store charges 7.5% sales tax on all orders including shipping.
Notes:
1. The local variable A will be used to designate the amount of goods purchased.
2. Since A + y% = A + x * y /100 = A * (1 + y /100), we will multiply the amount plus shipping by 1.075. (Let y = 7.5%)
3. The $5.95 shipping charge applies only when the order amount is less than $100.00. Set up the test this way: If A < 100, add 5.95 to A, otherwise do nothing. Because we are only executing further instructions only when the condition is true, no ELSE command is necessary.
The Program ONSALE
Comments will be italicized, starting with an asterisk. This program assumes that the purchase amount is on Level 1 of the stack.
[RS] [ + ] (<< >>)
* Start of the program
[RS] [ 0 ] (&rarr)
[ALPHA] [F1] (A)
* Assign the amount to the local variable A
[RS] [ + ] (<< >>)
* Start the main program
[ALPHA] [F1] (A)
* Call A to the stack
[LS] [EVAL] (PRG)
[F3] (BRCH)
[LS] [F1] (IF)
* Insert the IF-THEN-END structure
[ALPHA] [F1] (A)
[SPC] 100 [RS] [ X ] (<)
* Insert the test condition A < 100
[ &darr ] (down arrow)
5.95 [ + ]
* Add shipping charge if A < 100
[ &darr ] 1.075 [ x ] [RS] [ENTER] (&rarr NUM) [ENTER]
* Finish the program
[ ' ] [ALPHA] [ALPHA] [ ' ] (O) [EVAL] (N) [SIN] (S) [F1] (A) [NXT] (L) [F5] (E) [ENTER] [STO>]
The completed program ONSALE:
<< &rarr A << A IF A 100 < THEN 5.95 + END 1.075 * &rarr NUM >> >>
Instructions:
1. Enter the amount on Level 1 of the stack.
2. Run ONSALE.
Test Data:
Input = 50, Result = 60.14625 (A $50.00 order results in a total bill of $60.15.)
Input = 99.99, Result = 113.8855 (A $99.99 order results in a total bill of $113.89)
Input = 100, Result = 107.5 (A $100.00 order results in a total bill of $107.50. Surprised? Remember all orders $100.00 or more result in the shipping charged being waived.)
Input = 149.99, Result = 161.23925 ($149.99 order yields a bill of $161.24)
f(x) = (sin x)/x
This program calculates the function f(x) = (sin x)/x for all real numbers. If we attempt to calculate f(0) directly, we would get a "division by zero" error. However, the calculus limit as f(x) approaches 0 is 1. Let's create a test condition that detects for an input of 0. If the input is anything else, f(x) computes normally.
Note: The test of equality requires two equal signs, ==. This can be typed directly from the keyboard via alpha or by the TEST submenu of the PROGRAM menu. This program uses TEST submenu method (4 keystrokes opposed to 6 keystrokes).
The Program SINX
The program takes X from Level 1 of the stack and stores it as the local variable X.
[RS] [ + ] (<< >>)
[RS] [ 0 ] (&rarr)
[ X ]
* Stores the contents of Level 1 in the local variable X
[RS] [ + ] (<< >>)
[LS] [EVAL] (PRG)
[F3] (BRCH)
[RS] [F1] (IF)
* Inserts the IF-THEN-ELSE-END structure
[ X ] [SPC] 0
[LS] [EVAL] (PRG) [F4] (TEST) [F1] (==)
* Inserts the double equals sign, ==, for the equality test. Test X = 0?
[ &darr ] [SPC] 1
* Enters the commands should X = 0 (THEN)
[ &darr ] [SPC]
[ X ] [SIN] [ X ] [ ÷ ]
[RS] [ENTER] (&rarr NUM)
* Enters the commands should X ≠ 0 (ELSE)
[ENTER]
* Terminates program entry
[ ' ] [ALPHA] [ALPHA] [SIN] (S) [TOOL] (I) [EVAL] (N) [ X ] (X) [ENTER] [STO>]
The complete program:
<< &rarr X << IF X 0 == THEN 1 ELSE X SIN X / &rarr NUM END >> >>
Source: HP 48SX Scientific Expandable Calculator: Owner's Manual Volume II. Hewlett Packard, 3rd Edition, 1990
Instructions for SINX:
1. Enter X on the stack.
2. Run SINX.
Test Data - Assume the calculator is in Radians mode:
Input = -1, Result = 0.841470984808
Input = 0, Result = 1 (If you get this, then the IF-THEN-ELSE-END structure worked)
Input = 1, Result = 0.841470984808
Coming up, we'll take a look at the FOR-NEXT structure. See you next time in Part 4!
Eddie
This tutorial is property of Edward Shore. Mass reproduction or distribution requires express permission of the author.