Ad Space — Top Banner

?NEXT WITHOUT FOR ERROR

Commodore Commodore 64

Severity: Minor

What Does This Error Mean?

?NEXT WITHOUT FOR ERROR means the program reached a NEXT statement without a matching FOR statement. Every NEXT must have a corresponding FOR earlier in the program using the same variable.

Affected Models

  • Commodore 64
  • Commodore 64C
  • Commodore 128 (C64 mode)
  • VICE emulator (C64)

Common Causes

  • A NEXT statement with no matching FOR loop above it
  • FOR and NEXT using different variable names (e.g. FOR I ... NEXT J)
  • GOTO jumping into the middle of a loop, bypassing the FOR statement
  • FOR loop accidentally deleted while NEXT remains in the program

How to Fix It

  1. Check that every NEXT has a matching FOR with the same variable.

    FOR I = 1 TO 10 must be closed with NEXT I (or just NEXT). FOR I ... NEXT J will cause NEXT WITHOUT FOR because the variable names do not match.

  2. Count your FOR and NEXT statements — they must match in number.

    Type LIST and manually count the FOR lines and NEXT lines. If you have more NEXT than FOR, you have an extra NEXT somewhere.

  3. Make sure no GOTO jumps into the middle of a FOR loop.

    If a GOTO lands on a line inside a FOR..NEXT block but skips the FOR line itself, the interpreter has no loop context and NEXT fails.

  4. Indent your loops mentally to find the mismatch.

    Trace through the code: each FOR opens a level, each NEXT closes one. The first NEXT with no corresponding open FOR is the problem.

Frequently Asked Questions

Can I nest FOR loops on the Commodore 64?

Yes. FOR I = 1 TO 5 : FOR J = 1 TO 3 : ... : NEXT J : NEXT I is valid. The inner loop must be fully closed before the outer loop's NEXT.

Does NEXT need the variable name, or can I just write NEXT?

Both work. NEXT I and NEXT are equivalent for a single loop. For nested loops, naming the variable (NEXT J, NEXT I) makes the code clearer and avoids errors.