
In S11 you wrote a program that asked one question. To ask ten, you would copy the lines ten times - and to ask a hundred, you would not.
A loop does something a set number of times, and it keeps a count you can use while it does.
10 FOR I=1 TO 5
20 PRINT I;"SQUARED IS";I*I
30 NEXT I
FOR gives a variable a starting value. Everything down to NEXT happens, and then NEXT adds one and goes back - until the variable passes 5.
The end value is included. FOR I=1 TO 5 runs five times, with I being 1, 2, 3, 4 and 5. That is worth saying because the count inside the loop is what makes it useful: line 20 prints a different thing each time round without being written five times.
NEXT on its own closes the nearest loop, so NEXT and NEXT I do the same thing here. Naming the variable costs nothing and makes a long loop much easier to read, so this course names it.
STEP says what to add each time instead of 1:
50 FOR C=10 TO 1 STEP -1
60 PRINT C;
70 NEXT C
counts down. STEP 2 counts 1, 3, 5. The rule is the same either way: the loop goes on while the variable has not passed the end value.
After a loop finishes, its variable is one step past the end:
10 FOR I=1 TO 6 STEP 2
20 PRINT I;
30 NEXT
40 PRINT
50 PRINT "I IS NOW";I
prints 1 3 5 and then I IS NOW 7. The loop stopped because 7 is past 6, and 7 is what is left behind. Useful occasionally, surprising if you were not expecting it.
What does this do?
10 FOR I=5 TO 1
20 PRINT "BODY RAN WITH I=";I
30 NEXT I
Nothing, you would think - it is asking to count from 5 up to 1. In fact it runs once, printing BODY RAN WITH I= 5, and leaves I as 6.
The reason is that the test is made at the NEXT, not at the FOR. BASIC sets I to 5, runs the body, and only then asks whether to go round again.
A loop always runs at least once. So if you write FOR I=1 TO N where N might be 0, the body still happens - which is not what you meant. Test for it first, with an IF from S11.
A loop can contain another. Each needs its own variable:
10 FOR I=1 TO 3
20 FOR J=1 TO 2
30 PRINT I;J;
40 NEXT J
50 NEXT I
prints 1 1 1 2 2 1 2 2 3 1 3 2 - the inner loop running all the way through for each turn of the outer one.
Close them in the right order: the inner NEXT first. NEXT J,I closes both at once and is the same thing.
10 FOR I=1 TO 5
20 PRINT I;"SQUARED IS";I*I
30 NEXT I
40 PRINT "---"
50 FOR C=10 TO 1 STEP -1
60 PRINT C;
70 NEXT C
80 PRINT
90 PRINT "LIFT OFF"
Starting from
A fresh boot, or NEW.
What you should see
RUN
1 SQUARED IS 1
2 SQUARED IS 4
3 SQUARED IS 9
4 SQUARED IS 16
5 SQUARED IS 25
---
10 9 8 7 6 5 4 3 2 1
LIFT OFF
Ready

Line 60's trailing semicolon is what keeps the countdown on one line, and line 80's bare PRINT ends it. Without line 80, LIFT OFF would follow the 1 on the same row.
FOR I=5 TO 1 and run it. How many times does line 20 happen, and why is that not what "5 to 1" sounds like?100 PRINT "I ENDED AS";I and run the original. What is I, and where did that number come from?FOR C=10 TO 1 STEP -3. Predict what it prints before you run it.12.1 Print the seven times table, from 7 x 1 up to 7 x 12, one to a line.
12.2 Print a row of 20 stars using a loop, then do the same thing without a loop using MUL$ from S9. Which would you rather read?
12.3 Print a triangle five rows deep: one star on the first row, two on the second, up to five. You will need one loop inside another.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
Next Error in <line> | A NEXT with no FOR above it - often because the FOR is on a line that was never reached, or the loops were closed in the wrong order. |
| The loop ran once when it should not have run at all | The test is at the NEXT. A loop always runs once. Guard it with an IF. |
| The loop ran one time too few | The end value is included; check whether you wanted TO 10 or TO 9. |
| The variable is one too many afterwards | It is left one step past the end. That is normal. |
| Two loops interfere with one another | They are sharing a variable. Give each its own. |
| The loop never ends | Only a FOR loop with a STEP of 0 does that. Remember that under the emulator nothing stops a running program - S2 - so save first. |
FOR v=a TO b ... NEXT v repeats with a count from a to b, b included, and leaves the variable one step past b. STEP changes what is added, including backwards. The test happens at the NEXT, so a loop always runs at least once. Loops nest, innermost NEXT first.
S13, Round And Round - loops that run until something happens rather than a set number of times, and how to get out of one.