THE BEGINNER'S PAGE
Tom R. Halfhill, Editor
FOR-NEXT Applications
Last month we covered the basics of looping with the FOR-NEXT statement. Now let's take a look at some practical applications of this essential technique.
FOR-NEXT is such a general-purpose structure, it has numerous uses. Here's an example of how you might apply it in part of a checkbook-balancing program that sums the amounts for a month's worth of checks:
10 PRINT "HOW MANY CHECKS THIS MONTH"; 20 INPUT CH 30 FOR X = 1 TO CH 40 PRINT "AMOUNT OF CHECK"; 50 INPUT AM 60 SUM=SUM+AM 70 NEXT X 80 PRINT "TOTAL AMOUNT IS $";SUM
Let's take a careful look at this program. Try running it. Line 10 prompts the user to enter the number of checks to be added; line 20 stores the response in the variable CH. Line 30 is a little tricky. It marks the beginning of the loop with a FOR statement as shown in last month's examples, but the number of repetitions specified is a variable, not a number.
The variable, CH, contains the number of checks the user entered in response to the prompt. Therefore, the number of times the FOR-NEXT loop will repeat depends on the user's response. In effect, the program adapts itself to the user's needs. Think of what would happen if you specified the number of loops with a certain number, say 10. If the user has only 7 checks, the program would make too many loops, demanding amounts for 3 checks that were never written. If the user has 23 checks, the program would add only 10 of them together, ignoring the remaining 13.
Line 40 prompts the user to enter the dollar amount of the first check (do not type a dollar sign). Line 50 stores the response in the variable AM. Line 60 creates the variable SUM to keep track of the total and adds AM to SUM (when the first pass through the loop begins, SUM equals 0).
Line 70 marks the end of the loop. It circles back to the FOR statement on line 30. Now the loop begins its second pass. Again, a prompt asks the user to input a check amount. Again, the response is stored in the variable AM (replacing the previous amount for the first check). Again, AM is added to SUM, so SUM now contains the cumulative amounts of the first and second checks. And again, at line 70, NEXT X circles back to line 30 for the third pass.
This continues until the number of loops specified by CH is reached. Then the loop is done and the program proceeds to line 80. The program prints the total amount held in SUM and ends.
This example shows two things: first, the usefulness of FOR-NEXT loops when combined with other techniques, such as INPUT statements; and second, the flexibility of FOR-NEXT loops when modified slightly, such as specifying the number of loops with a variable instead of a particular number.
Speed READing
One of the most common applications of FOR-NEXT loops is to combine them with the READ and DATA statements. (If you aren't familiar with READ-DATA, don't worry; it's a subject for a future column.) By embedding a READ statement within a loop, you can efficiently fill an array with DATA, or POKE numbers for custom character sets or machine language subroutines into memory.
Let's try a simple example. Say you're writing some sort of calendar program that requires the computer to print a column of numbers representing the number of days in each month of the year. Without looping, you could take this approach:
10 PRINT "31" 20 PRINT "28" 30 PRINT "31" 40 PRINT "30" 50 PRINT "31" 60 PRINT "30" 70 PRINT "31" 80 PRINT "31" 90 PRINT "30" 100 PRINT "31" 110 PRINT "30" 120 PRINT "31"
But a FOR-NEXT loop with READ-DATA is much more efficient:
10 FOR X = 1 TO 12 20 READ A 30 PRINT A 40 NEXT X 50 DATA 31,28,31,30,31,30, 31,31,30,31,30,31
Line 10 sets up a FOR-NEXT loop with 12 passes (the number of elements in the DATA statement at line 50). Line 20 reads a number from the DATA statement and stores it in the variable A.
Line 30 prints the value of A, which changes after each pass through the loop. During the first pass, A equals 31 (the number of days in January) because 31 is the first DATA element. During the second pass, A equals 28 (the number of days in February) because 28 is the second DATA element. This continues for all 12 passes, concluding when A is assigned the value 31 for the twelfth month, December.
Next month, we'll continue our discussion of FOR-NEXT by showing how to put loops within loops, and even why you might want to create a loop that does absolutely nothing. We'll also cover some variations of FOR-NEXT in different versions of BASIC.