Classic Computer Magazine Archive COMPUTE! ISSUE 20 / JANUARY 1982 / PAGE 22

The Beginner's Page


Richard Mansfield Assistant Editor

You'll hear the term algorithm from time to time. It merely means a procedure, a way of getting something done. For example, let's assume that your programming becomes so impressive that you decide to start a software business. You want to generate a list of possible names for your new venture and then pick out the best one. You could make a list yourself, but you are a programmer and you have a computer which could make your list in a jiffy. All you need to figure out is the algorithm: the steps your computer needs to follow to create the list. Most algorithms, especially for jobs involving lists, use loops.

First put all your favorite words about software into a table of DATA statements. This will give the computer something from which to make its list. Then, you use a nested loop to combine the data in all possible ways.

Loop Forms

The loop is one of the primary ways that a computer does its work: FOR I = 1 TO 10. (Do something. Print the variable I, for example). NEXT I. This structure means: as long as I is still between 1 and 10, print I on the screen. Raise the value of I by one (NEXT I) and loop (jump back to the FOR statement which will check to see if I is still within bounds). We, ourselves, loop every day (and we ask others to loop for us), but we don't think of it as looping. If you were about to make a list (by hand), you might start off by taking a sheet of paper and writing down the numbers 1. 2. 3. etc. along one side. This is precisely the loop in our example above.

Another common loop form is "please find me the map; it's in that pile." (FOR I = 1 TO 50: IF X$(I) = "MAP" THEN PRINT "HERE IT IS.": NEXT I) Of course, when you use the "IF" structure, you cannot put NEXT I on the same line. If you did, the NEXT part would only loop IF X$(I) = "MAP." Anything following IF is governed by that IF and will not be carried out unless the IF comes true.

"Will you please wait two seconds before telling me your name?" (FOR I = 1 TO 2000: NEXT I: PRINT "MY NAME IS COMPUTER.") This is called a delay loop because the computer does nothing between the FOR and the NEXT. It just waits until it counts to 2000 which takes about two seconds.


If you put a loop within a loop, the inner one is called a nested loop. "Ask all six people in this room what their three favorite foods are." (FOR I = 1 TO 6: FOR J = 1 TO 3: PRINT "WHAT'S A FAVORITE FOOD OF YOURS?": NEXT J: NEXT I) It's easiest to grasp nested loops by working from the inner loop out. The J loop is asking the question three times before it transfers the control back to its master loop I. The total number of loopings (iterations is the technical term) will be 18 (I's iterations multiplied by J's).

Why do we use I for our counting variables in loops? It's just conventional. It must have once meant increments or iterations or index, but that hardly matters. It is convenient because you can then remember never to use I elsewhere in your programs for other variables—I is always your master loop variable. Then, logically, it is common practice to use J for a nested loop within the I loop. Also, for timing delay loops, it is a good habit to reserve the variable T as we did above. T, of course, stands for Time. It is not used anywhere else in programs (for the same reasons).

Picking A Company Name

Our algorithm for listing possible company names is a nested loop. We picked eight adjectives we liked and came up with seven nouns. This means we have two lists which we want to combine into one list. We put the adjectives and nouns into their own separate DATA lines and READ them into two arrays. An array is a table or list—a grouping of items which are somehow related to each other so we want them stored together under the same name. In this case we set up two string arrays: ADJECTIVE$ #1 through #8 and NOUN$ #1 through #7. The loop in line 120 hangs unique tags on each word in the DATA statement as it reads and memorizes each item. For example, when it READs "super" it tags it with the variable name ADJECTIVE$(2). If you finished RUNning the program and directly asked the computer ? ADJECTIVE$(5) it would print "QUALITY." For information on string arrays on the Atari see COMPUTE! #11 pg. 103 and COMPUTE! #16 pg. 36.

Knowing that putting a noun before an adjective usually results in nonsense (apple red) we decided to refine our list of potential names for our company by only permitting adjectives to modify nouns. This means we want to list a noun and go through all eight possible adjectives for it before listing the next noun. This is very like asking six people to name three favorite foods.

The nesting is in lines 140 to 180. Notice that the NEXT J will always loop back to line 150 until the FOR J condition (count up to eight) is satisfied. Then the program will execute the NEXT I.

Can we nest at even deeper levels? Sure. Typing a new line: 165 FOR T = 1 TO 2000: NEXT T will provide a short delay loop between each item as it appears on the screen. Could we see the list backwards? Change two lines: 140 FOR I = 7 TO 1 STEP -1 and: 150 FOR J = 8 TO 1 STEP -1. Every other name? 150 FOR J = 1 TO 8 STEP 2. Only names beginning with the letter A? 155 IF LEFT$ (ADJECTIVE$(J),1) <> "A" THEN GOTO 170. (For Atari: 165 IF ADJECTIVE$ (J*20-19, J*20-19 <> "A" THEN 180)

As you can see, all kinds of choices, refinements, or modifications are possible within loops by merely changing a few instructions to the machine. The combination of loops and branches (lines starting with IF or ON) coupled with the computer's great speed (you try to count from one to 2000 in two seconds) is the essence of the great power of computers.

Microsoft Version

130 FOR I = 1 TO 7: READ NOUN$ (I): NEXT I
140 FOR I = 1 TO 7
150 FOR J = 1 TO 8
170 NEXT J
180 NEXT I

Atari Version

120 DIM ADJECTIVE$ (8*20), NOUN$(7*2 P$(20), L1(8), L2(7)
130 FOR I=1 TO 8:READ TEMP$: ADJECT *20-19, I*20)=TEMP$:L1(I)=LENK TEMP$: I
140 FOR I=1 TO 7 : READ TEMP$ : NOUN$(9, I *20)=TEMP$:L2(I)=LENK TEMP$): NEX
150 FOR I=1 TO 7
160 FOR J=1 TO 8
170 PRINT ADJECTIVE$((J-1)*20+1,(J+L1(J));"";NOUNS((I-1)*20+1, (I-1)(I))
180 NEXT J
190 NEXT I