Section 6 - Looping One of the great powers of computer and calculator programs comes from the ability to repeat a sequence of commands as many times as may be necessary. The HP49G+ provides five different looping structures, only three of which will be discussed here. Those interested in the other looping forms should consult pages 21-53 through 21-63 of UG.. The first looping structure we will explore is the WHILE structure. It has the form WHILE condition REPEAT block END The condition here is the same as in Section 5 and the REPEAT command works very much like the THEN command discussed in Section 5. When the program reaches REPEAT the object is removed from level 1 of the stack, if it is true (i.e. any nonzero real number) the block of instructions is executed then the program returns to the condition and tests it again. Notice that something must happen to eventually make the condition false, or the program will loop forever. When the condition is false (i.e., the object on level 1 is a zero), the program jumps to the first command after the END. All the elements for the WHILE structure can be found in LS PRG F3-BRCH F6-WHILE, but as with IF, LS F6-WHILE from the BRCH menu will produce the whole WHILE...REPEAT...END structure. As an example we will write a program that is a variation on our earlier program to compute our bowling average. In this case we will assume that we are simply out for an evening of bowling with friends and don't know how many games we will bowl. We will have the calculator prompt the user for the next score until a negative score is entered, which will be a signal (called a sentinel) that there are no more scores. << 0 0 "Enter first score." "" INPUT Save this program as BOL1 and single step through it at least twice, once with several scores before the -1 is entered and once with -1 entered as the first score. See how it all works. The second looping procedure we will consider is the FOR structure, which has the form first last FOR index block NEXT In this structure first is an integer that represents the first value of the loop index, last is an integer that represents the last
value of the loop index, and index is the loop index and is a local variable that lives only while the loop is in effect.
WARNING: Because As an example, we shall rewrite BOL1 to prompt the user for the number of games played, then ask for exactly that number of scores. << "How many games?" "" INPUT Save this program as BOL2 and single step through it at least twice, once with a positive first input and once with zero as the first input. It won't show on this program because it is protected by the IF statement, but if last starts smaller than first the FOR loop will still be executed once. A minor variation of the FOR loop structure is the START loop structure: first last START block NEXT This is used to loop a fixed number of times when the value of the index is not needed. We could, for example have written the previous program as << "How many games?" "" INPUT This way, however, the prompt is not quite as user friendly. This version also demonstrates that it was not necessary to use the local variable n. Save this program as BOL3 and try it with a positive for the number of games and with 0 for the number of games. The START...NEXT structure can also be created from the BRCH menu with LS F3-START. EXERCISE SET 6 1. Change Problem 1 of Exercise Set 4 to prompt the user to enter the price of each item the customer is purchasing until a zero is entered. (We are assuming that nothing costs $0, so it can serve as a sentinel.) It should then display the subtotal, the tax, and the total as tagged output. Call this version TAX4. 2. Professor Dinklesmith gives 10 quizzes during the semester and the final grade is the average of the 10 scores. Each quiz is worth 100 points. An average of 90 or better is an A, 80 or more but less than 90 is a B, 70 or more but less than 80 is a C, 60 or more but less than 70 is a D, and less than 60 is an F. Write a program that will prompt the user for the ten scores then output the average with the appropriate letter grade as a tag. For example, if the average is 87.3, the output should be B:87.3. Call this program GRD.
|