FOR without NEXT
Sinclair ZX Spectrum
Severity: MinorWhat Does This Error Mean?
If your FOR loop runs only once or the program ends unexpectedly, the NEXT statement may be missing or unreachable. Unlike some BASICs, the Spectrum does not always give an explicit error — it may just show 0 OK and stop. Check that every FOR has a matching NEXT with the correct variable name.
Affected Models
- Sinclair ZX Spectrum 48K
- Sinclair ZX Spectrum 128K
- Spectrum+
- Spectrum +2
- Spectrum +3
- ZX Spectrum Next
- Fuse emulator
Common Causes
- NEXT statement missing for a FOR loop
- NEXT uses the wrong variable name — FOR I but NEXT J
- GOTO or GOSUB jumps out of the loop before reaching NEXT
- IF statement skips over the NEXT line
- Nested loops not properly closed — last opened must be first closed
How to Fix It
-
Check that every FOR has a matching NEXT with the same variable.
FOR I = 1 TO 10 must end with NEXT I. FOR J = 1 TO 5 inside it must have NEXT J before NEXT I.
-
Make sure no GOTO jumps out of the loop without reaching NEXT.
If GOTO skips past NEXT, the loop body runs once but never repeats. Either restructure the logic or use GOTO to jump back to the FOR line.
-
Check nested loop order: last opened, first closed.
Correct: FOR I, FOR J, NEXT J, NEXT I. Wrong: FOR I, FOR J, NEXT I, NEXT J.
-
Make sure NEXT is not inside an IF that might be skipped.
IF condition THEN NEXT I — if the condition is false, NEXT is skipped. Put NEXT on its own line outside any IF statement.
Frequently Asked Questions
Does the Spectrum give an error if NEXT is missing?
Not always. If the program reaches the end without hitting NEXT, it simply stops with 0 OK. The error 1 NEXT without FOR only appears when NEXT is found without a matching FOR — not the other way around.
Can I exit a FOR loop early on the Spectrum?
There is no BREAK or EXIT FOR command. To exit early, set the loop variable to the end value and let NEXT handle it naturally. For example: LET I = 10 before NEXT I exits a FOR I = 1 TO 10 loop.