Tuesday, October 18, 2011

RPL Programming Tutorial - Part 11 - HP 49g+/50g: The CASE Structure and Labeling Results

In this case, do this; in that case, do that...

Part 11 of our tutorial will cover the CASE structure, which is really the IF-THEN-ELSE-END structure repeated more than one time.

The CASE structure looks like this:

CASE test_1 THEN commands if test_1 is true END
test_2 THEN commands if test_2 is true END
test_3 THEN commands if test_3 is true END
...
commands if none of the tests are true (default, optional) END


The default commands are optional. If there is no default commands, the structure ends without any action. Note that CASE is only needed once, and there must always be a final END to this structure. CASE is found in the PRG-BRCH menu.

Key [LS] [F2] (CASE) to get:

CASE
THEN
END
END

Use this key sequence as a starter.

Key [RS] [F2] (CASE) to get:

THEN
END

Use this key sequence to add additional cases. Remember only one CASE is needed per structure.

Labeling Results

Sometimes we want to label results. One way to label results is the relatively easy command →TAG. This command can be accessed by the keystroke sequence:

[LS] [EVAL] (PRG) [F5] (TYPE) [F5] (→TAG)

→TAG takes the result from Level 2 and a string (your label) from Level 1. An example would be:

2: 3.14159265359
1: "PI"

Executing →TAG returns:

1: PI:3.14159265359

→TAG automatically adds a colon ( : ) to the label. A tagged object (number) can be used in any calculation like normal. However once a calculation is executed, the tag is lost.

What's my tax?

To illustrate the CASE structure, let's analyze and calculate how much income tax liability taxpayers have in a given state. The state takes the taxable income of a taxpayer and uses this graduated schedule to determine payment:

$0 - $14,999: 5% of taxable income
$15,000 - $99,999: 7% of taxable income exceeding $14,999 plus $1,050
$100,000 and above: 9% of taxable income exceeding $99,999 plus $7,000

Here the tax preparer wants to know how much is the tax liability. In addition, the preparer wants to label both the income and tax liability.

The CASE structure is set up like this:

Case 1: If income < 15,000, then calculate income * 5%
Case 2: If 15,000 ≤ income < 100,000, then calculate income above 14,999 * 7% + 1050, We'll break up the condition for Case 2 this way: 15,000 ≤ income AND income < 100,000.
Otherwise: Calculate income above 99,999 * 9% + 7000

The Program INTAX

[RS] [ + ] (<< >>)
[LS] [SYMB] (MTH) [F5] (REAL) 0 [NXT] [NXT] [F1] (RND)

* Rounds the entry to the nearest integer (number 0 RND)
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F2] (CASE)
* The primary CASE-THEN-END-END structure
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
15000 [RS] [big X] ( < )

* First condition: income < 15,000
[ ↓ ] [F1] (DUP) .05 [ x ]
* First calculation
[ ↓ ]
[F1] (DUP) [F1] (DUP) 100000 [RS] [big X] ( < ) [F2] (SWAP) 15000 [LS] [1/X] ( ≥ )
[LS] [EVAL] (PRG) [F4] (TEST) [NXT] [F1] (AND)
[LS] [EVAL] (PRG) [F3] (BRCH) [RS] [F2] (CASE)

* Second condition: 15,000 ≤ income < 100,000 - connect two required conditions with AND
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP) 14999 [ - ] .07 [ x ] 1050 [ + ]
* Second calculation
[F1] (DUP) 99999 [ - ] .09 [ x ] 7000 [ + ]
* Default calculation
[ ↓ ]
* Exits the CASE structure
0 [LS] [SYMB] (MTH) [F5] (REAL) [NXT] [NXT] [F1] (RND)
* Rounds the final result to the nearest integer
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [COS] (T) [F1] (A) [big X] [ALPHA] [ → ]
[LS] [EVAL] (PRG) [F5] (TYPE) [F5] (→TAG)

* Apply the tag on the income tax.
[LS] [EVAL] (PRG) [F1] (STACK) [F2] (SWAP)
[RS] [ x ] [ALPHA] [ALPHA] [TOOL] (I) [EVAL] (N) [F3] (C) [ ' ] (O) [HIST] (M) [F5] (E) [ALPHA] [ → ] [LS] [EVAL] (PRG) [F5] (TYPE) [F5] (→TAG)

* Apply the tag on the income.
[LS] [EVAL] (PRG) [F1] (STACK) [F2] (SWAP) [ENTER]
* Finish the program

[ ' ] [ALPHA] [ALPHA] [TOOL] (I) [EVAL] (N) [COS] (T) [F1] (A) [big X] [ENTER] [STO>]

The completed program:

<< 0 RND
CASE DUP 15000 < THEN DUP .05 * END
DUP DUP 100000 < SWAP 15000 ≥ AND THEN
DUP 14999 - .07 * 1050 + END
DUP 99999 - .09 * 7000 + END
0 RND "TAX" →TAG SWAP "INCOME" →TAG SWAP >>


Instructions:

1. Enter the taxable income.
2. Run INTAX

Examples:

1. Income: $13,000; Tax: $650
2. Income: $15,000; Tax: $1,050
3. Income: $50,000; Tax: $3,500
4. Income: $100,000; Tax: $7,000
5. Income: $150,000; Tax: $11,500

This wraps it up for Part 11 - on to Part 12 where we work with a subroutine. Until next time, Eddie.

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