Ad Space — Top Banner

?NEXT WITHOUT FOR ERROR

Commodore VIC-20

Severity: Minor

What Does This Error Mean?

?NEXT WITHOUT FOR ERROR means the VIC-20 encountered a NEXT statement with no matching FOR loop. Check that every NEXT has a FOR above it using the same variable name.

Affected Models

  • Commodore VIC-20

Common Causes

  • FOR line deleted but NEXT remains
  • NEXT uses a different variable than FOR (e.g. FOR I ... NEXT J)
  • A GOTO jumps into the loop body, skipping the FOR initialisation
  • Nested loops with incorrectly ordered NEXT statements

How to Fix It

  1. Find the NEXT statement causing the error.

    The error message includes the line number. Type LIST and locate that NEXT statement.

  2. Trace upward to find the matching FOR.

    From the NEXT line, scroll up through the listing to find a FOR that uses the same variable. If none exists, either add the FOR or delete the orphaned NEXT.

  3. Make sure FOR and NEXT use the same variable.

    FOR I = 1 TO 10 must end with NEXT I (or just NEXT). NEXT J when the loop variable is I causes this error. Fix by changing NEXT J to NEXT I.

  4. Check nested loop order.

    Nested loops must close in reverse order: the last opened FOR must have the first NEXT. Incorrect order: FOR A / FOR B / NEXT A / NEXT B gives ?NEXT WITHOUT FOR. Correct: FOR A / FOR B / NEXT B / NEXT A.

Frequently Asked Questions

Can I use NEXT without a variable name on the VIC-20?

Yes. In Commodore BASIC V2, NEXT without a variable closes the innermost open FOR loop. However, always naming the variable (NEXT I) makes the code easier to read and debug.

Does the VIC-20 have a way to check for open loops?

No built-in debug tools. The best technique is to add PRINT statements before each FOR and NEXT during testing to confirm the flow is as expected.