HP 12c Owners Manual
Have a look at the manual HP 12c Owners 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: Programming Basics 101 File name: hp 12c_users guide_English_HDPMBF12E44 Page: 101 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm If the duration of the pause is not long enough to write down the number displayed, you can prolong it by using more than one u instruction. Alternatively, you can have the program automatically stop as described next. Stopping Program Execution Stopping Program Execution Automatically. Program execution is automatically halted when the program executes a t instruction. To resume executing the program from the program line at which execution was halted, press t . Example: Replace the program above by one containing t instructions instead of u instructions. Keystrokes Display fs 00- Sets calculator to Program mode. fCLEARÎ 00- Clears program memory. § 01- 20 t 02- 31Stops program execution to display AMOUNT. ?+1 03- 44 40 1 :0 04- 45 0 b 05- 25 t 06- 31Stops program execution to display TAX. ?+2 07- 44 40 2 + 08- 40 ?+3 09- 44 40 3 fs 6,370.52 Sets calculator to Run mode. fCLEAR² 0.00 Clears registers R1 through R6. 13\68.5 68.5 First item. t 890.50 AMOUNT for first item. t 60.11 TAX for first item. t 950.61 TOTAL for first item. 18\72.9 72.9 Second item. t 1,312.20 AMOUNT for second item. t 88.57 TAX for second item. t 1,400.77 TOTAL for second item.
102 Section 8: Programming Basics File name: hp 12c_users guide_English_HDPMBF12E44 Page: 102 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm Keystrokes Display 24\85 85. Third item. t 2,040.00 AMOUNT for third item. t 137.70 TAX for third item. t 2,177.70 TOTAL for third item. 5\345 345. Fourth item. t 1,725.00 AMOUNT for fourth item. t 116.44 TAX for fourth item. t 1,841.44 TOTAL for fourth item. :1 5,967.70 Sum of AMOUNT column. :2 402.82 Sum of TAX column. :3 6,370.52 Sum of TOTAL column. Program execution is also automatically halted when the calculator overflows (refer to page 73) or attempts an improper operation that results in an Error display. Either of these conditions signifies that the program itself probably contains an error. To determine at which program line execution has halted (in order to locate the error), press any key to clear the Error display, then press fs to set the calculator to Program mode and display that program line. You may also want to display the current program line (by pressing fs ) if your program has halted at one of several t instructions in your program and you want to determine which one that is. To continue executing the program afterward: 1. Press fs to set the calculator back to Run mode. 2. If you want to resume execution from the program line at which execution halted rather than from line 00, press gi followed by two digit keys that specify the program line desired. 3. Press t to resume execution. Stopping Program Execution Manually. Pressing any key while a program is running halts program execution. You may want to do this if the calculated results displayed by a running program appear to be incorrect (indicating that the program itself is incorrect). To halt program execution during a pause in a running program (that is, when u is executed), press any key. After stopping program execution manually, you can determine at which program line execution has halted and/or resume program execution as described above.
103 File name: hp 12c_users guide_English_HDPMBF12E44 Page: 103 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm Section 9 Branching and Looping Although the instructions in a program normally are executed in order of their program line numbers, in some situations it is desirable to have program execution transfer or “branch” to a program line that is not the next line in program memory. Branching also makes it possible to automatically execute portions of a program more than once — a process called “looping.” Simple Branching The i (go to) instruction is used in a program to transfer execution to any program line. The program line desired is specified by keying its two-digit line number into the program line containing the i instruction. When the i instruction is executed, program execution branches or “goes to” the program line specified and then continues sequentially as usual. You have already seen a common use of branching: the i 00 instruction (that is stored in program memory after the program you key in) transfers execution to program line 00. A i instruction can be used to branch not only backward in program memory — as in the case of i 00 and as illustrated above — but also forward in program memory. Backward branching is typically done to create loops (as described next); forward branching is typically done in conjunction with an o or m instruction for conditional branching (as described afterward).
104 Section 9: Branching and Looping File name: hp 12c_users guide_English_HDPMBF12E44 Page: 104 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm Looping If a i instruction specifies a lower-numbered line in program memory, the instructions in the program lines between the specified line and the i instruction will be executed repeatedly. As can be seen in the illustration above under Simple Branching, once the program begins executing the “loop” it will execute it again and again. If you want to terminate the execution of a loop, you can include an o or m instruction (described below) or an t instruction within the loop. You can also terminate execution by pressing any key while the loop is being executed. Example: The following program automatically amortizes the payments on a home mortgage without requiring you to press f! for each payment. It will amortize one month’s payments each time or one year’s payments each time the loop is executed, depending on whether the number 1 or 12 is in the display when you start running the program. Before running the program, we’ll “initialize” it by storing the required data in the financial registers — just as we would do if we were amortizing a single payment manually. We’ll run the program for a $50,000 mortgage at 12 3/4% for 30 years, and we’ll key 1 into the display just before running it in order to amortize monthly payments. For the first two “passes” through the loop we’ll execute the program one line at a time, using Ç , so that we can see the looping occurring; then we’ll use t to execute the entire loop a third time before terminating execution. Keystrokes Display fs 00- Sets calculator to Program mode. fCLEARÎ 00- Clears program memory ?0 01- 44 0Stores the number from the display into register R 0. This number will be the number of payments to be amortized.
Section 9: Branching and Looping 105 File name: hp 12c_users guide_English_HDPMBF12E44 Page: 105 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm Keystrokes Display :0 02- 45 0Recalls the number of payments to be amortized. This program line is the one to which pro gram execution will later branch. It is included because after the first time the loop is executed, the number in the “display” * is replaced by the result of ! . f! 03- 42 11Amortizes payment(s). gu 04- 43 31Pauses to display amount of payment(s) applied to interest. ~ 05- 34Brings amount of payment(s) applied to principal into “display.”* gu 06- 43 31Pauses to display amount of payment(s) applied to principal. gi02 07- 43, 33 02Transfers program execution to line 02, so that the number of payments to be amortized can be recalled to the display before the ! instruction in line 03 is executed. fs 0.00 Sets calculator to Run mode. (Display shown assumes no results remain from previous calculations.) fCLEARG 0.00 Clears financial registers. 30gA 360.00 Enters n. 12.75gC 1.06 Enters i. 50000$ 50,000.00 Enters PV. g 50,000.00 Sets payment to End. P –543.35 Calculates the monthly payment. 0n 0.00 Reset n to zero. 1 1. Keys 1 into the display to amortize monthly payments. Ê 01- 44 0Line 01: ? 0. 1.00 * More precisely, the number in the X-register.
106 Section 9: Branching and Looping File name: hp 12c_users guide_English_HDPMBF12E44 Page: 106 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm Keystrokes Display Ê 02- 45 0Line 02: : 0. This is the beginning of the first pass through the loop. 1.00 Ê 03- 42 11Line 03: f! . –531.25 Portion of first month’s payment applied to interest. Ê 04- 43 31Line 04: gu . –531.25 Ê 05- 34Line 05: ~ . –12.10 Portion of first month’s payment applied to principal. Ê 06- 43 31Line 06: gu . –12.10 Ê 07- 43, 33 02Line 07: gi 02. This is the end of the first pass through the loop. –12.10 Ê 02- 45 0Line 02: : 0. Program execution has branched to the beginning of the loop for the second pass throu gh it. 1.00 Ê 03- 42 11Line 03: f! . –531.12 Portion of second month’s payment applied to interest. Ê 04- 43 31Line 04: gu . –531.12 Ê 05- 34Line 05: ~ . –12.23 Portion of second month’s payment applied to principal. Ê 06- 43 31Line 06: gu . –12.23
Section 9: Branching and Looping 107 File name: hp 12c_users guide_English_HDPMBF12E44 Page: 107 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm Keystrokes Display Ê 07- 43, 33 02Line 07: gi 02. This is the end of the second pass through the loop. –12.23 t –530.99 Portion of third month’s payment applied to interest. –12.36 Portion of third month’s payment applied to principal. t(or any key) –12.36 Halts program execution. Conditional Branching Often there are situations when it is desirable for a program to be able to branch to different lines in program memory, depending on certain conditions. For example, a program used by an accountant to calculate taxes might need to branch to different program lines depending on the tax rate for the particular income level. The hp 12c provides two conditional test instructions that are used in programs for conditional branching: z o tests whether the number in the X-register (represented by the x in the key symbol) is less than or equal to the number in the Y-register (represented by the y in the key symbol). As discussed in Appendix A, the number in the X-register is simply the number that would, if the calculator were in Run mode, be currently in the display; and the number in the Y-register is the number that would, if the calculator were in Run mode, have been in the display when \ was pressed. For example, pressing 4\5 would place the number 4 in the Y-register and the number 5 in the X-register. z m tests whether the number in the X-register is equal to zero. The possible results of executing either of these instructions are: z If the condition tested for is true when the instruction is executed, program execution continues sequentially with the instruction in the next line of program memory. z If the condition tested for is false when the instruction is executed, program execution skips the instruction in the next line of program memory and continues with the instruction in the following line. These rules can be summarized as “DO if TRUE”.
108 Section 9: Branching and Looping File name: hp 12c_users guide_English_HDPMBF12E44 Page: 108 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm The program line immediately following that containing the conditional test instruction can contain any instruction; however, the most commonly used instruction there is i . If a i instruction follows a conditional test instruction, program execution branches elsewhere in program memory if the condition is true and continues with the next line in program memory if the condition is false. Example: The following program calculates income tax at a rate of 20% on incomes of $20,000 or less and 25% on incomes of more than $20,000. To conserve program lines, the program assumes that the test value — 20,000 — has been stored in register R 0 and the tax rates — 20 and 25 — have been stored in registers R 1 and R2, respectively. Note: If a program requires that certain numbers be in the X- and Y-registers when instructions such as o are executed, it is extremely helpful when writing the program to show the quantities in each register after each instruction is executed, as in the following diagram.
Section 9: Branching and Looping 109 File name: hp 12c_users guide_English_HDPMBF12E44 Page: 109 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm We’ll key the income into the display before running the program so that it will be in the X-register when the : 0 instruction in program line 01 is executed. This instruction will place the test value 20,000 in the X-register and (as explained in Appendix A) move the income into the Y-register. The ~ instruction in program line 02 will exchange the numbers in the X- and Y-registers (as also explained in Appendix A): that is, it will place the income back into the X-register and place the test value into the Y-register. This is necessary because when either the : 2 instruction in line 05 or the : 1 instruction in line 07 is executed, the number in the X-register is moved into the Y-register; if the ~ instruction were not included, the test value 20,000, rather than the income, would be in the Y-register when the b instruction in line 08 is executed. Keystrokes Display fs 07- 43, 33 02Sets calculator to Program mode. (Display shows program line at which execution was halted at end of preceding example.) fCLEARÎ 00- Clears program memory. :0 01- 45 0Recalls test value into X-register and places income in Y-register. ~ 02- 34Places income in X-register and test value in Y-register. go 03- 43 34Tests whether number in X-register (income) is less than or equal to number in Y-register (20,000).
110 Section 9: Branching and Looping File name: hp 12c_users guide_English_HDPMBF12E44 Page: 110 of 209 Printered Date: 2005/7/29 Dimension: 14.8 cm x 21 cm Keystrokes Display gi07 04- 43, 33 07If condition is true, branches to program line 07. :2 05- 45 2If condition is false, recalls 25% tax rate to X-register. gi08 06- 43, 33 08Branches to program line 08. :1 07- 45 1Recalls 20% tax rate to X-register. b 08- 25Calculates tax. fs –12.36 Sets calculator to Run mode. (Display shows results of running of previous program.) Now, well store the required numbers in registers R 0, R1, and R2, then we’ll run the program, using Ç so that we can check that the branching occurs properly. It’s good practice with programs containing conditional test instructions to check that the program branches correctly for all possible conditions: in this case, if the income is less than, equal to, or greater than the test value. Keystrokes Display 20000?0 20,000.00 Stores test value in register R0. 20?1 20.00 Stores 20% tax rate in register R1. 25?2 25.00 Stores 25% tax rate in register R2. 15000 15,000. Keys income less than test value into display and X-register. Ê 01- 45 0Line 01: : 0. 20,000.00 Test value has been recalled to X-register, moving income to Y-register. Ê 02- 34Line 02: ~ 15,000.00 Income has been placed in X-register and test value has been placed in Y-register. Ê 03- 43 34Line 03: go 15,000.00 Ê 04- 43, 33 07Condition tested by o was true, so program execution continued with line 04: gi 07.