The HP48 Homepage  

News My Programs Software Programming Articles Links Sitemap Search Feedback    

Programming in User RPL - Loop structures

In the previous page, you learned a form of control structure: the conditionals. They allow a part of a program to be executed only if a condition is met. Now, you'll see other types of control structures: the loop structures. They allow part of a program to repeat a definite or indefinite number of times.

Indefinite Loop Structures

The first kind of loop structure we'll see is the Indefinite Loop. As the name says, it repeats an indefinite number of times, while or until a condition is met. There are two forms of indefinite loops in the HP48. WHILE...REPEAT...END loops and DO...UNTIL...END loops. Basically, they do the same thing: repeat the code while a certain condition is met, or until a condition is met. The form of the WHILE structure is:

       test condition
       code to be repeated

When te HP sees WHILE in a program, it evaluates the test condition. If it is true, then the code to be executed is executed, and when END is reached, the program re-starts the process at WHILE. If the test condition evaluates false, then the program resumes after the END. Since the condition is evaluated before the main code, it may never be executed, if the condition first evaluates false.

Let's see a simple example of WHILE. The program calculates the sum of the even numbers between 100 and 200.

    << 0           @ Initial sum
       1           @ First number
       -> sum n    @ We could manipulate the stack, but let's
                   @ use local variables to make things easier
       << WHILE
             n 200 <
          REPEAT   @ Repeating while the sum hasn't reached 200
             n 'sum' STO+   @ Increment sum
             'n' 2 STO+     @ Increment count
             @ Oh, yes, STO+ accepts its arguments in any order
          sum    @ Output the sum

The other form of indefinite loop is DO. It's behavour is like WHILE. But instead of repeating while the condition is true, it repeats until the condition is true. Or, while it's false. The form of it is:

       code to be executed
       test condition

The code to be executed will be executed at least once, until test condition evaluates true. A note for both WHILE and DO: if you use something like


the program will be executed until your batteries have completely drained or the program is stopped. So be careful that sometime the condition must stop the loop.

Now, let's see the above program, rewritten for DO. Only the essential has changed, the rest remained the same.

    << 0 100 -> sum n
       << DO
             'sum' n STO+
             2 'n' STO+
             n 200 >

Definite Loop Structures

Opposing to the structures above, which will run forever if you let them, the Definite Loop Structures run a determined number of times, no more and no less. There are two definite loop structures on the HP48, and they are very similar, differing only in a detail.

The first one is START...NEXT. It's form is:

    start stop START
       commands to be executed

START reads two numbers from the stack, the initial number and the final number. Then, the commands to e executed are executed. When NEXT is reached, the initial nubmer is incremeted by one. If it is smaller or equal to the final number, then the commands will be executed again. If it is greater, then execution will continue after NEXT.

And, finally, the second definite loop structure is FOR...NEXT. It is exactly like START, with one difference: you can acess the number of that execution and use it. The sintaxis is:

    start stop FOR var
       commands to be executed

The variable can have any name, and works like any local variable. Traditionally, the variable is called i, j or k. But this is only a suggestion.

In START and FOR structures, you can change NEXT by

    increment STEP

The difference is that the count will be increment by increment instead of 1.

Now, let's see the above program, in it's most efficient (well, amost) form:

    << 0           @ Initial sum
       100 200     @ Goes from 100 to 200
       FOR i       @ The current number is i
          i +      @ Increment sum by i
       2 STEP      @ Increment by 2, since we want only even numbers


  1. Running the following program, which results do you get?
    << { } 10
          DUP 1 - 3 ROLLD SQ + SWAP
          DUP 1 ==
  2. Modifiy the above program to use a WHILE structure. (Change as little as possible)
  3. Write a program to calculate the fatorial of a number on the stack.
  4. Suppose Country A has a population of 30,000,000 inhabitants and an annual growth rate of 3%; and Country B has a population of 200,000,000 inhabitants and an annual growth rate of 1.5%. Write a program (using DO) to calculate the number of years necessary for the population of Country A get equal or greter than the popultion of country B. Output the number of years, plus the population of both countries.
  5. A radioactive chemical element loses half of its mass every 50 seconds. Given the initial mass in grams on the stack, write a program to determine the time necessary for the mass get less than 0.5 grams. Output the final mass and the time in hours, minutes and seconds in the form "xh ymin zs"
  6. Write programs to calculate the following sums:
  7. The following programs calculate sums of fractions. Write the sums in the form of the exercise above, and also using sigma notation.
    1. << 0 DUP
         14 FOR a
            2 a ^ 15 a - SQ /
            -1 a ^ * +
    2. << 100
         1 99 FOR b
            100 b - b ! +
    3. << 63
         1 31 FOR i
            63 i 2 * - i ! / +
    4. << 1
         2 20 FOR j
            j ! 2 j ^ 1 - /
            -1 j ^ * -
  8. Write a program to calculate pi using

    where . Use 51 terms.

  9. Write a program to calculate the value of pi, with a precision of .01. Use DO and:

  10. Write a program that, when x is on the stack, returns ex, with a precision of 10-11. Use WHILE.

  11. Supposing x is on the stack, write programs to calculate the following sums:
  12. Write a program that calcultes the approximate square root of a number Y on the stack, using Newton's sucessive approximantions method:
    • the first approximation of v/(Y) is Y/2
    • the sucessive approximations are
    The program should calculate 20 approximations.
  13. Modify the above program so that it calculates the square root with a precision of 10-11.
  14. Write a program to find a root of a equation using Newton's Method:

    Assume a equation is on level 3, the variable on level 2 and a initial guess X0 is on level 1. Calculate 30 approximations.

  15. Modify the above program to search for a root with a precision specified by the user, on level 1 (all other values are shifted one level up).

Answers to exercises

Previous Page Next Page

This page was created by Eduardo M Kalinowski