( 13 )
Previous Table Of Contents Next


Loops

We have already seen infinite loops in the IncrementForever example programs. But those program loops weren't particularly worthy, as no useful work was being done. We motivated the SecondOrderEq example programs by imagining that you had an assignment to plot the second order equation:

     y = f(x) = x2 + 2 * x + 3

for the 21 data points over the range of integers from x = -10 to +10. We then wrote a few flavors of the program that solved for a single data point each time you ran the program. But a program loop, which is a structure that causes a certain section of the program to repeat over and over, can simplify the operator's job of getting his 21 data points. If we ask the operator to use the SecondOrderEq2.txt program he will need to make at least 3 * 21 keystrokes to get his 21 results because he must enter each x axis value and then click GSB 0.

But because the x axis values are predictable we can write a new program which will automatically advance the x axis value and thus save the operator keystrokes. The operator would start this new program by typing -10, GSB 0 and would then receive the y data point corresponding to x = -10. By merely clicking R/S (Run/Stop) to re-initiate the program where it left off, the operator would receive the y data point corresponding to x = -9. Additional R/S keystrokes would produce additional data points.

All we need to do to accomplish this is to increment x by 1 before looping back to the beginning of the program. Although we could store x on the operand stack, I will show you a program that stores x in data memory as this arrangement should be simpler to understand. The new SecondOrderEq4.txt program is shown below:

; file SecondOrderEq4.txt ; This RPN calculator program computes the second order eq.: ; y = f(x) = 1x^2 + 2x + 3 ; To run the program enter an x value into the X stack location ; and then type GSB 0 (alternatively, type GTO .00 followed ; by R/S). When the program stops the corresponding y value ; is visible in the X stack location. You can then get additional ; y values simply by clicking on R/S. Each new y value ; corresponds to an x value 1 larger than the former x value. ; This program employs Reg[0] to hold the sequence of x values. LBL 0 ; program entry point, at this time X = x STO 0 ; preserve initial x value in Reg[0] LBL 1 ; we loop back to here for each new computation x^2 ; X = x^2 +2. ; X = 2, Y = x^2 ENTER ; X = 2, Y = 2, Z = x^2 RCL 0 ; X = x, Y = 2, Z = x^2 * ; X = 2x, Y = x^2 +3. ; X = 3, Y = 2x, Z = x^2 + ; X = 2x+3, Y = x^2 + ; X = x^2+2x+3 R/S ; show result to operator RCL 0 ; recall original x +1. ; increment x by 1 + STO 0 ; save new x GTO 1 ; loop back for another iteration

The following four new lines at the end of the program increment the x value by 1. Remember that we have chosen to keep the x axis value in Reg[0] (i.e., data memory location 0).

RCL 0 ; recall original x +1. ; increment x by 1 + STO 0 ; save new x

After advancing the x axis value this program branches back for another iteration (repetition of the code in the loop). This is accomplished by the GTO 1 statement which returns control to the LBL 1 statement. Every time we pass the LBL 1 statement we have a new x axis value and we are about to compute:

     y = f(x) = x2 + 2 * x + 3

Every time the program encounters the R/S statement it stops in order to show the operator the most recent result (the new y axis value).

Programmers often use flow charts to illustrate the flow of control in their programs. A flow chart for the SecondOrderEq4.txt program would be:

Most computer programs, if viewed from 20,000 feet, look similar to this flow chart. About the only change that you sometimes see is that if the program has a way to exit from the loop then it will perform some clean-up duties before shutting down.

The SecondOrderEq4.txt program produces a new y value every time the operator clicks on the R/S key. Let's again modify this program so that the operator can continue to get additional data points without hitting any keys! The trick is to remove the R/S statement and replace it with a PAUSE statement. A PAUSE statement causes the calculator's program to halt for 1 second and then proceed. Hence each new y value will be visible in the X stack position for (at least) 1 second. If this gives you enough time to write it down then you don't have to touch the calculator at all. If you want a longer delay then insert 2 PAUSE statements. Or, if the phone rings and you want to temporarily halt the program simply click on the R/S (or any other) key. After you hang up the phone again click on R/S and the program will continue. This version can be found in the SecondOrderEq5.txt file and looks like:

; file SecondOrderEq5.txt ; This RPN calculator program computes the second order eq.: ; y = f(x) = 1x^2 + 2x + 3 ; To run the program enter an x value into the X stack location ; and then type GSB 0 (alternatively, type GTO .00 followed ; by R/S). After a moment of computation the corresponding y value ; will be visible in the X stack location. Additional y values ; corresponding to a sequence of x values spaced apart by 1 ; will continue to appear every second. ; This program employs Reg[0] to hold the sequence of x values. LBL 0 ; program entry point, at this time X = x STO 0 ; preserve initial x value in Reg[0] LBL 1 ; we loop back to here for each new computation x^2 ; X = x^2 +2. ; X = 2, Y = x^2 ENTER ; X = 2, Y = 2, Z = x^2 RCL 0 ; X = x, Y = 2, Z = x^2 * ; X = 2x, Y = x^2 +3. ; X = 3, Y = 2x, Z = x^2 + ; X = 2x+3, Y = x^2 + ; X = x^2+2x+3 PAUSE ; show result to operator RCL 0 ; recall original x +1. ; increment x by 1 + STO 0 ; save new x GTO 1 ; loop back for another iteration

Remember that calculator programs continue to run until you hit a key (any key). The only keys which will start a program running are R/S, GSB n, and GSB i (we haven't introduced this last one yet).


Previous Table Of Contents Next