Counting
Use FOR/NEXT to count up, count down, and count in steps — the loop that drives every game.
Over the next eight units you’ll build Colour Flood — a memory game where coloured panels flash in sequence and you repeat the pattern. The sequence starts short and grows with every correct answer. But first, you need to understand the tool that makes sequences possible: the FOR/NEXT loop.
Count to Ten
Type NEW, then enter this program:
10 FOR i=1 TO 10
20 PRINT i
30 NEXT i
Type RUN. Numbers march down the screen — 1, 2, 3, all the way to 10, each on its own line. Three lines do all the work:
FOR i=1 TO 10creates a counter callediand sets it to 1.PRINT iprints whateveriholds right now.NEXT iadds 1 toiand jumps back to the line after FOR. Whenipasses 10, the loop ends and the program moves on.
The variable i changes every time the loop runs. That is the key insight — the same PRINT statement produces different output on each pass because i holds a different number.
Count Backwards
Type NEW and enter:
10 FOR i=10 TO 1 STEP -1
20 PRINT i
30 NEXT i
40 PRINT "Go!"
Type RUN. The screen counts down — 10, 9, 8… 2, 1 — then “Go!” appears at the bottom. A three-line countdown timer.
STEP -1 makes the counter decrease instead of increase. Without STEP, the counter always goes up by 1. With STEP -1, it goes down by 1. The loop ends when i drops below the target (1 in this case).
Count in Twos
10 FOR i=0 TO 20 STEP 2
20 PRINT i;" ";
30 NEXT i
Type RUN. You see 0, 2, 4, 6… 20. Only the even numbers appear. STEP 2 skips every other value. The semicolon and space after i in the PRINT statement put each number on the same line with a gap between them — a handy trick for compact output.
You can use any step value. STEP 5 would give 0, 5, 10, 15, 20. STEP 3 would give 0, 3, 6, 9, 12, 15, 18.
Try This
- Change the first program to
FOR i=1 TO 100. The numbers fill the screen faster than you can read them. That is the Spectrum doing something a hundred times in under a second. - Try
FOR i=1 TO 1. The loop runs exactly once.FOR i=1 TO 0? It does not run at all — the start is already past the end.
What You’ve Learnt
- FOR/NEXT repeats code a set number of times with an automatic counter
- Loop variable —
icounts up (or down) on each pass and you can use it inside the loop - STEP controls how much the counter changes each time (default is 1)
- Counting backwards —
STEP -1makes the loop count down instead of up