Home > HP > Calculator > HP 15c Manual

HP 15c Manual

    Download as PDF Print this page Share this page

    Have a look at the manual HP 15c Manual online for free. It’s possible to download the document as PDF or print. UserManuals.tech offer 1114 HP manuals and user’s guides for free. Share the user manual or guide on Facebook, Twitter or Google+.

    							 Section 8: Program Branching and Controls 91 
     
    of this loop can be  controlled by a  conditional  branch, an ¦ instruction 
    (written  into  the  loop),  or  simply  by  pressing  any  key  during  execution 
    (which stops the program). 
     
    Conditional Tests 
    Another way to alter the  sequence of program execution is by a conditional 
    test, a  true/false  test  which  compares  the  number  in  the  X-register  either  to 
    zero  or  to  the  number  in  the  Y-register.  The  HP-15C  provides  12  different 
    tests,  two  explicit  on  the  keyboard  and  10  others  accessible  using | 
    T n.* 
    1. Direct: | £ and | ~ . 
    2. Indirect: | T n. 
     
    n Test n Test 
    0 x ≠ 0 5 x = y 
    1 x > 0 6 x ≠ y 
    2 x < 0 7 x > y 
    3 x ≥ 0 8 x < y 
    4 x ≤ 0 9 x ≥ y 
                                                               * Four of the conditional tests can also be used for complex values, as explained in section 11 on page 132.   
    						
    							92 Section 8: Program Branching and Controls 
     
    Following  a  conditional  test,  program  execution  follows  the  Do  if  True 
    Rule:  it  proceeds sequentially  if  the  condition  is true,  and  it skips  one 
    instruction if the condition is false. A t instruction is often placed right 
    after  a  conditional  test,  making  it  a conditional  branch; that  is,  the t 
    branch is executed only if the test condition is met. 
     
    Flags 
    Another  conditional  test  for  programming  is  a flag test.  A  flag  is  a  status 
    indicator  that  is  either set (=  true)  or clear (=  false).  Again,  execution 
    follows the Do if True Rule: it proceeds sequentially if the flag is set, and 
    skips one line if the flag is clear. 
    The  HP-15C  has  eight user flags,  numbered  0  to  7,  and  two system flags, 
    numbered 8 (Complex  mode)  and 9 (overflow condition). The  system flags 
    are discussed later in this section. All flags can be set, cleared, and tested as 
    follows: 
     | F n will set flag number n (0 to 9). 
     |  n will clear flag number n. 
     | ? n will check if flag n is set. 
    A  flag n that  has  been  set  remains  set  until  it  is  cleared  either  by  a  
    n instruction or by clearing (resetting) Continuous Memory.   
    						
    							 Section 8: Program Branching and Controls 93 
     
    Examples 
    Example: Branching and Looping 
    A  radiobiology  lab  wants  to  predict  the 
    diminishing  radioactivity  of  a  test  amount  of 131I,  a  radioisotope.  Write  a  program  to  figure 
    the  radioactivity  at  3-day  intervals  until  a 
    given limit is reached. The formula for Nt, the 
    amount of radioisotope remaining after t days, 
    is 
    Nt = No (2-t/k), 
    where k = 8 days, the half-life of 131I, and N0 is the initial amount. 
    The  following  program  uses  a  loop  to  calculate  the  number  of  millicuries 
    (mci)  of  isotope  theoretically  remaining  at  3-day  intervals  of  decay. 
    Included is a conditional  test to check the  result and end the program  when 
    radioactivity has fallen to a given value (a limit). 
    The program assumes t1 – the first day of measurement – is stored in R0, N0 
    – the  initial  amount  of  isotope – is  stored  in  R1, and  the  limit  value  for 
    radioactivity is stored in R2. 
    Keystrokes Display  
    | ¥ 000- Program mode. 
    ´ CLEAR M 000- (Optional.) 
    ´ b A  001-42,21,11 Each loop returns to this line. 
    l 0 002-   45  0 Recalls current t which 
    changes with each loop. 
    ´ © 003-   42 31 Pauses to display t. 
    8 004-       8 k 
    ÷ 005-      10  
    “ 006-      16 –t/k. 
    2 007-       2  
    ®  008-      34  
    Y 009-      14 2–t/k.   
    						
    							94 Section 8: Program Branching and Controls 
     
     
    Keystrokes Display  
    l * 1 010-45,20, 1 Recall multiplication with the contents of R1 (N0), yielding Nt, 
    the mci of 131I remaining after t days 
    ´© 011-   42 31 Pauses to display Nt. 
    l 2 012-   45  2 Recalls limit value to X-register. 
    | T 9 013-43,30, 9 x ≥ y ? Tests whether limit value 
    (in X) meets or exceeds Nt  (in Y). 
    | n 014-   43 32 If so, program ends. 
    3 015-       3 If not, program continues. 
    O+ 0 016-44,40, 0 Adds 3 days to t in R0. 
    tA 017-   22 11 Go to ―A‖ and repeat execution to find a new Nt from a new t. 
    Notice  that  without  lines  012  to  014,  the  loop would  run  indefinitely  (until 
    stopped from the keyboard). 
    Lets run the program, using t1 = 2 days, N0 = 100 mci, and a limit value of 
    half of N0 (50 mci). 
    Keystrokes Display  
    | ¥  Run mode (display will vary). 
    2 O 0 2.0000 t1.  
    100 O 1 100.0000 N0. 
    50 O 2 50.0000 Limit value for Nt. 
    ´A 2.0000 t1. 
     84.0896 N1. 
     5.0000 t2. 
     64.8420 N2. 
     8.0000 t3. 
     50.0000 N3. 
     50.0000 Nt limit; program ends.  
    						
    							 Section 8: Program Branching and Controls 95 
     
    Example: Flags 
    Calculations  on  debts  or investments  can  be  calculated  in  two  ways:  for 
    payments  made  in  advance  (at  the  beginning  of  a  given  period)  and  for 
    payments  made  in  arrears  (at  the  end  of  a  given  period).  If  you  write  a 
    program  to  calculate  the  value  (or  ―present  value‖)  of  a  debt  or  investment 
    with  periodic  interest  and  periodic  payments,  you  can  use  a  flag  as  a  status 
    indicator  to  tell  the  program  whether  to  assume  payments  are  made  in 
    advance or payments are made in arrears. 
    Suppose you are planning the payment of your childs future college tuition. 
    You  expect  the  cost  to  be  about  $3,000/year  or  about  $250/month.  If  you 
    wanted  to  withdraw  the  monthly  payments  from  a  bank  account  yielding 
    6%  per  year,  compounded  monthly  (which  equals  0.5%  per  month),  how 
    much  must  you  deposit  in  the  account  at  the  start  of  the  college  years  to 
    fund monthly payments for the next 4 years? 
    The formula is 
     if  payments  are  to  be  made 
    each month in advance, 
    and the formula is  
     if  payments  are  to  be  made 
    each month in arrears. 
    V is the total value of the deposit you must make in the account; 
    P is the size of the periodic payment you will draw from the account; 
    i is the periodic interest rate (here: ―periodic‖ means monthly, since interest 
    is compounded monthly); and 
    n is the number of compounding periods (months). 
    The  following  program  allows  for  either  payment  mode.  It  assumes  that, 
    before the program is run, P is in the  Z-register, n is in the Y-register, and i 
    is in the X-register. )(1)(11ii
    i PVn
    
    
    
    
     
    
    
    
    
    
    i
    i PVn)(11  
    						
    							96 Section 8: Program Branching and Controls 
     
     
    Keystrokes Display  
    | ¥ 000- Program mode. 
    ´ bB 001-42,21,12 Start at B if payments to be 
    made at the beginning. 
    |  0 002-43, 5, 0 Flag 0 clear (false); indicates advance payments. 
    t 1 003-   22  1 Go to main routine. 
    ´ b E 004-42,21,15 Start at E if payments to be 
    made at the end. 
    | F 0 005-43, 4, 0 Flag 0 set (true); indicates 
    payment in arrears. 
    ´ b 1 006-42,21, 1 Routine 1 (main routine). 
    O1 007-   44  1 Stores i (from X-register). 
    1 008-       1  
    + 009-      40 (1+i). 
    ® 010-      34 Puts n in X; (l + i) in Y. 
    “ 011-      16 – n. 
    y 012-      14 (1 + i)-n. 
    “ 013-      16 – (1 + i)-n. 
    1 014-       1  
    + 015-      40 1 – (1 + i)-n. 
    l ÷ 1 016-45,10, 1 Recall division with R1 (i) to 
    get [l– (l + i)-n]/i. 
    * 017-      20 Multiplies quantity by P. 
    | ? 0 018-43, 6, 0 Flag 0 set? 
    | n 019-   43 32 End of calculation if flag 0 set 
    (for payments in arrears). 
    l 1 020-   45  1 Recalls i. 
    1 021-       1  
    + 022-      40 (1 + i). 
    * 023-      20 Multiplies quantity by final 
    term. 
    | n 024-   43 32 End of calculation if flag 0 clear.  
    						
    							 Section 8: Program Branching and Controls 97 
     
    Now  run  the  program  to  find  the  total  amount  needed  in  an  account  from 
    which  you  want  to  take  $250/month  for  48  months.  Enter  the  periodic 
    interest  rate  as  a  decimal  fraction,  that  is,  0.005  per  month.  First  find  the 
    sum  needed  if  payments  will  be  made  at  the  beginning  of  the  month 
    (payments  in  advance),  then  calculate  the  sum  needed  if  payments  will  be 
    made at the end of the month (in arrears). 
    Keystrokes Display  
    |¥  Set to Run mode. 
    250 v 250.0000 Monthly payment. 
    48 v 48.0000 Payment periods (4 years × 12 
    months). 
    .005 0.005 Monthly interest rate as a 
    decimal fraction. 
    ´ B 10,698.3049 Deposit necessary for 
    payments to be made in 
    advance. 
    (Repeat stack entries.)   
    ´ E 10,645.0795 Deposit necessary for payment to 
    be made in arrears. (The 
    difference between this deposit 
    and the tuition cost ($12,000) 
    represents interest earned on the 
    deposit!) 
    Further Information 
    Go to 
    In  contrast  to  the  nonprogrammable  sequence t “ nnn,  the 
    programmable  sequence t label cannot  be  used  to  branch  to  a  line 
    number,  but  only  to program  label (a  line  containing ´ b label).* 
    Execution continues from  the  point of the  new label, and does not return to 
    the original routine unless given another t instruction.  
    t label can  also  be  used  in  Run  mode  (that  is,  from the  keyboard)  to 
    move to a labeled position in program memory. No execution occurs. 
                                                               * It  is  possible  to  branch  under  program  control  to  a  particular  line number by  using  indirect  addressing, discussed in section 10.  
    						
    							98 Section 8: Program Branching and Controls 
     
    Looping 
    Looping  is  an  application  of  branching  which  uses  a t instruction  to 
    repeat a portion of the program. A loop can continue indefinitely, or may be 
    conditional.  A  loop  is  frequently  used  to  repeat  a  calculation  with  different 
    variables.  At  the  same  time,  a  counter,  which  increments  with  each  loop, 
    may  be  included  to  keep  track  of  loop  iterations.  This  counter  can  then  be 
    checked  with a  conditional test to determine  when to exit the  loop. (This is 
    shown in the example on page 112.) 
    Conditional Branching 
    There  are  two  general  applications  for  conditional  branching.  One  is  to 
    control  loops,  as  explained  above.  A  conditional  test  can  check  for  either  a 
    certain calculated value or a certain loop count. 
    The  other  major use  is to test  for options and pursue  one.  For example, if a 
    salesperson  made  a  variable  commission  depending  on  the  amount  of  sale, 
    you could write a program which takes the amount of sale, compares it to a 
    test value, and then calculates a specific commission depending on whether 
    the sale is less than or greater than the test value. 
    Tests. A conditional test takes what is in the X-register (“x”) and compares 
    it  either  to  zero  (such  as ~)  or  to “y”, that  is,  what  is  in  the  Y-register 
    (such as £). For an x:y comparison, therefore, you must have the x- and 
    y-values  juxtaposed  in  the  X- and  Y-registers.  This  might  require  that  you 
    store  a  test  value  and  then  recall  it  (bringing  it  into  the  X-register).  Or,  the 
    value  might be  in the  stack and be  moved, as  necessary, using ®, ), 
    or (. 
    Tests  With  Complex  Numbers  and  Matrix  Descriptors. Four  of  the 
    conditional  tests  also  work  with  complex  numbers  and  matrix  descriptors: 
    ~, T 0  (x≠ 0), T 5  (x = y), and T 6  (x≠ y).  Refer  to 
    sections 11 and 12 for more information. 
    Flags 
    As  a  conditional  test  can  be  used  to  pick  an  option  by  comparing 
    two numbers  in  a  program,  a  flag  can  be  used  to  pick  an  option externally. 
    Usually,  a  flag  is  set  or cleared  first  thing  in  a  program  by  choosing  a 
    different  starting  point  (using  different  labels)  depending  on  the  condition 
    or mode you want (refer to the example on page 95).  
    						
    							 Section 8: Program Branching and Controls 99 
     
    In this way, a program can accommodate two different modes of input, such 
    as  degrees  and  radians,  and  make  the  correct  calculation  for  the  mode 
    chosen.  You  set  a  flag  if  a  conversion  needs  to  be  made,  for  instance,  and 
    clear it if no conversion is needed. 
    Suppose you had an equation requiring temperature input in degrees Kelvin, 
    although sometimes your data might be in degrees Celsius. You could use a 
    program with a flag to allow either a Kelvin or Celsius input. In part, such a 
    program might include: 
    ´ bC Start program at ―C‖ for degrees Celsius.=
    |  7 Flag 7 cleared (=false). 
    t 1  
    ´ b Á Start program at ―D‖ for degrees Kelvin.=
    | F 7 Flag 7 set (=true). 
    ´ b 1 (Assuming temperature in X-register.) 
    | ? 7 Checks for flag 7 (checks for Celsius or Kelvin 
    input). 
    t 2 If set (Kelvin input), goes to a later routine, skipping 
    the next few instructions. 
    2 If cleared (Celsius input), adds 273 to the 
    7 value in the X-register, since °K = °C + 273. 
    3  
    +  
    ´ b 2 Calculation continues for both modes. 
    ⋮ 
    The System Flags: Flags 8 and 9 
    Flag 8. Setting flag 8 will activate Complex mode (described in section 11), 
    turning on the C annunciator. If another method is used to activate Complex 
    mode,  flag  8  will  automatically  be  set.  Complex  mode  is  deactivated  only 
    by clearing flag 8; flag 8 is cleared in the same manner as the other flags.  
    						
    							100 Section 8: Program Branching and Controls 
     
    Flag  9. An  overflow  condition  (described  on  page  61)  automatically  sets 
    flag  9.  Flag  9  causes  the  display  to  blink  or,  if  a  program  is  running,  waits 
    until execution is complete and then starts blinking the display. 
    Flag 9 may be cleared in three ways: 
     Press |  9 (the common procedure for clearing flags). 
     Press −. This will only clear flag 9 and stop the blinking—it will 
    not clear the display. 
     Turn the calculator off. (Flag 9 is not cleared if the calculator turns 
    itself off.) 
    If you set flag 9 manually (F 9), it causes the display to blink irrespective 
    of  the  overflow  status  of  the  calculator.  As  usual,  a  program  will  run  to 
    completion before  the  display starts blinking. Therefore,  flag 9 can be  used 
    as a programming tool to provide a visual signal for a selected condition.  
    						
    All HP manuals Comments (0)