Previous Section
Cover Page
Table of Contents
Index
Exercises for this Section
Next Section

Section 5 - Branching

We will discuss two general types of branching, IF and CASE. The IF structure has the form:

	IF condition THEN
	    block 1
	ELSE
	    block 2
	END

The condition is an expression that is either true or false and each of the blocks is a set of one or more instructions. If the condition is true then all the instructions in block 1 are executed, if the condition is false, then all the instructions in block 2 are executed. The ELSE block 2 is optional.

The CASE structure has the form

	CASE
	    condition 1 THEN
	                  block 1
	                END
	    condition 2 THEN
	                  block 2
	                END
	           .
	           .
	           .
	    condition n THEN
	                  block n
	                END
	    block n + 1
	END

The block of instructions corresponding to the first true condition encountered is the only block that will be executed. If none of the conditions are true, block n + 1 will be executed. This last block is optional.

Before we can continue with the IF and CASE structure, we need to understand how the HP49G+ works with boolean values and boolean operations, that is, with values that are either true or false. LS PRG F4-TEST brings up a menu with six relational functions (five of these functions are also on the keyboard). Each of the functions takes values from level 2 and level 1 and returns a 1 if the relation is true and 0 if the relation is false. For example, put 5 on level 2 and 8 on level 1. If you press F3-< or RS < you get a 1 and if you press F4-> or RS > you get a 0. F1-== in this menu and RS = on the keyboard are NOT the same. In the HP language, = is used to assign a function to its name, == is used to test equality between two values. The == can be created in a program from the keyboard by AS(hold) RS = RS = with the alpha shift held for the four following keystrokes.

Press NXT to get to the next page of this menu and you find the commands AND, OR, XOR, and NOT. These four operations are the same on the calculator as they are in logic; AND is true if both conditions are true, OR is true if at least one condition is true, XOR is true if exactly one of the conditions is true and NOT is true if the condition is false. The first three of these are binary functions which act on levels 1 and 2 of the stack. AND returns 1 if both values are 1's and 0 otherwise. OR returns 1 if at least one of the values is a 1, and 0 otherwise. XOR returns 1 if exactly one of the values is 1 and the other is 0, and returns 0 otherwise. NOT toggles the value on level 1 between 0 and 1. It should be noted that these four logical functions and also the THEN command which we will discuss in the next paragraph, treat any nonzero real number as a 1. If, for example, you place .5 on level 2 of the stack and -3.7 on level 1 and press AND the response will be a 1.

We are now ready to return to the IF and CASE structures. The critical element here is the THEN statement. When the calculator comes to THEN it takes the value off of level 1 of the stack. If the value is true (that is, any real number not equal to zero), it executes the block of instruction immediately following, and at the end of that block goes to the very end of the IF or CASE structure. If the value is false (i.e., a zero) the program skips to the next section of the structure it is working through; in the case of the IF statement it goes to the block after the ELSE if there is one, or to the END of the IF if there is no ELSE; in a CASE structure it goes to the next condition, to block n + 1, or the END of the CASE, whichever comes first.

LS PRG F3-BRCH F1-IF gets one to the menu with the IF elements and LS PRG F3-BRCH F2-CASE to the menu with the CASE elements, but that's not the best way to get those structures into a program. From the BRCH menu LS F1-IF will put IF ... THEN ... END into the program and RS F1-IF will put IF ... THEN ... ELSE ... END into the program. LS F2-CASE will put CASE ... THEN ... END ... END into the program and each RS F2-CASE will put in an additional THEN ... END. These typing aids make the job easier and help eliminate the very common error of forgetting to put in an END statement.

We will write a program to calculate a person's pay for a week. The input will be the hourly rate and the hours worked. If the hours worked is greater than 40, the person is to receive an extra half pay for the hours over 40.

	<<
	   "Hours worked" "" INPUT 
	   "Pay rate" "" INPUT 
	    h pr
	      <<
	         h pr *
	         IF h 40 > THEN
	            h 40 - pr 2 / *  +
	         END
	      >>
	   "Gross pay" 
	>>

Enter this program and save it as GPAY. Single step through the program three times with the hours worked less than, equal to, and greater than 40.

We will now write a new version of BWL3 that will use an IF statement to overcome the problem of a negative handicap if the bowler's average is greater than 200.

	<<
	   SCR1 SCR2 + SCR3 + "Series total = " OVER + MSGBOX
	   3 / FLOOR "Average = " OVER + MSGBOX
	   IF DUP 200 < THEN
	      200 SWAP - .8 * FLOOR
	   ELSE
	      DROP 0
	   END
	   "New handicap = " OVER + MSGBOX 'HNCP' STO
	>>

Save this program as BWL5 and single step through it a couple of times, once with the average less that 200 and once with it over 200. BWL3 is actually a more efficient program than this one, but this gives us a good example of how the IF statement works, and it is easier to read this program and understand what it is doing.

For an example of a CASE structure we will write a program to solve the quadratic equation ax2 + bx + c = 0. We will assume we are only interested in real solutions, so we will write the program to output a message about the type of roots, but only give the values in the case a double root or of two distinct real roots.

<<
   "Enter the leading  coefficient" "" INPUT 
   "Enter the coefficient  of the linear term" "" INPUT 
   "Enter the constant  term" "" INPUT 
   0  a b c d
      <<
         b SQ 4 a * c * - 'd' STO
         CASE
            d 0 > THEN
                    "The roots are " b NEG d  + 2 a * /
                    DUP
                    IF FP 0 == THEN
                       
                    END
                    + " and " + b NEG d  - 2 a * /
                    DUP UNROT + SWAP
                    IF FP 0 THEN
                       "." +
                    END
                  END
            d 0 == THEN
                     "There is a double root at " b NEG 2 a * /
                     DUP UNROT + SWAP
                     IF FP 0 THEN
                        "." +
                     END
                   END
           "The roots are complex."
         END
      >>
   MSGBOX
>>

The NEG function to change the sign of the element in level 1 can be entered by pressing the +/- key. The "extra" IF statements that include the function FP are there to make the output "pretty." FP can be found at LS MTH F5-REAL NXT F6-FP. It is explained on page 3-14 of UG. Save this program as QDEQ and single step through it with different values of a, b, and c so that you can see all three paths of the CASE in action. Be sure to try examples with both integer and non-integer roots to see why the "extra" IF statements and the use of FP and were necessary.

Tuition at Podunk University is $25000 per year. A student who is the child of a PU employee or the child of a clergy gets a 10% discount. If the student is the child of both a PU employee and a clergy, the discount is 15%. We will write a program that will ask if the student fits either of those conditions, then compute the bill.

	<<
	   "Child of employee?  Enter Y or N" { "" } INPUT
	   "Child of clergy?  Enter Y or N" { "" } INPUT
	   1  e c m
	   <<
	      CASE
	         e "Y" == c "Y" == AND THEN .85 'm' STO END
	         e "Y" == c "Y" == OR THEN .9 'm' STO END
	      END
	      25000 m *
	      >>
	   "Tuition" 
	>>

The coding { "" } puts the calculator into alphabetic mode before the INPUT is executed, so the user need only press the appropriate letter key, without pressing AS first. The question mark, ?, can be entered by AS RS 3. Save this program as TUIT and single step through it with various choices of input to see how the AND and OR work.

EXERCISE SET 5

1. Add the coding necessary to your solution of Problem 2 of Exercise Set 4 so that if the time to the desired exit is more than four hours a message box will warn the user of the need to plan an intermediate rest stop. Call this program TMR4.

2. Write a program to check a customer's credit availability. The user should be prompted to enter the customer's outstanding balance, current purchase, and credit limit. If the balance plus the current purchase is less than or equal to the credit limit a message box will say the purchase is approved, otherwise it will disapprove the purchase. Call this program CRD

3. Change the example GPAY above to compute withholdings and net pay. If the employee earned no more than $50, there is no withholding. If the earnings are more than $50 but no more than $300, withhold 10% of the excess over $50. If the earnings are over $300, but no more than $500, withhold $25 plus 15% of the excess over $300. If the earnings are over $500, withhold $55 plus 20% of the excess over $500. There should be three lines of tagged output giving the gross wages, the withholding, and the net wages. Call this program NPAY1

4. Change Problem 3 of Exercise Set 4 so that if x is not between x1 and x2 inclusive a message box will give the user a warning that the program is extrapolating. Save this program as LIN3.

Previous Section
Cover Page
Table of Contents
Index
Top of Page
Next Section