FOR...NEXT is a loop that always goes around a specific number of times. It puts how many times it has gone around in a variable you give it, which can be any numeric variable you want, but is generally I, J, or X. Generally, it looks like this:
FOR X = 1 TO num
instructions
NEXT X
Here is an example of a very simple program, which counts from 1 to 50:
FOR I = 1 TO 50 PRINT I, NEXT I
Note that the counting variable is no longer X, but is now I. This code simply prints the variable I 50 times, because we have told it to do everything between the FOR and the NEXT 50 times. I changes every time, so it prints a different number each time. Now here is a program that uses a variable to tell the FOR how many times to repeat:
INPUT "Launch how many rockets? ", launch
FOR J = 1 TO launch
PRINT "Rocket "; J; " launched!"
SLEEP 1
NEXT J
(Download)
This is a sample run:
Launch how many rockets? +5+ Rocket 1 launched! Rocket 2 launched! Rocket 3 launched! Rocket 4 launched! Rocket 5 launched!
The SLEEP 1 simply tells QBASIC to pause for 1 second, to keep the rockets from all launching at once. Of course, we don't need to put PRINT into the loop. Nor do we need to put the variable we use for counting into the loop. Here is another program, which just asks for the ages of a group of people, then averages them.
INPUT "How many people? ", people
FOR I = 1 TO people
INPUT "Age of person? ",age
total = total + age
NEXT I
average = total / people
PRINT "Average age: ";
PRINT USING "##.#"; average
(Download)