NEXT Without FOR
Sharp Sharp MZ
Severity: MinorWhat Does This Error Mean?
NEXT without FOR on the Sharp MZ means the interpreter hit a NEXT statement without a corresponding FOR loop above it. This is almost always a deleted FOR line, a mismatched loop variable, or a GOTO that skips into the loop body.
Affected Models
- Sharp MZ-80B
- Sharp MZ-80K
- Sharp MZ-700
- Sharp MZ-800
- Sharp MZ-1500
Common Causes
- FOR line deleted but NEXT remains in the program
- NEXT uses a different variable name than the FOR statement
- A GOTO or GOSUB jumps into the loop body past the FOR
- Nested loops with NEXT statements in the wrong order
How to Fix It
-
List the program and find the NEXT at the reported line.
Use LIST to view the program. Go to the line number in the error and identify the orphaned NEXT statement.
-
Trace upward to find a matching FOR.
From the NEXT line, scroll up through the listing looking for a FOR that uses the same variable. If no matching FOR exists, either add it or remove the NEXT.
-
Verify the loop variable is the same in FOR and NEXT.
FOR I = 1 TO 10 must end with NEXT I. If the NEXT says J, that is the mismatch — change it to I.
-
Check that no GOTO jumps inside the loop body.
A GOTO that lands between the FOR and NEXT lines (without going through the FOR) means the FOR counter is never set up. The NEXT then has nothing to count down — causing this error.
Frequently Asked Questions
Does HuBASIC support NEXT without a variable name?
Yes. In HuBASIC (used on the MZ-700/800), NEXT without a variable closes the innermost open FOR. However, always naming the variable makes debugging easier on older hardware where re-listing is slow.
How do I debug loop errors on the Sharp MZ?
Add PRINT statements inside the loop to confirm the loop variable value. For example: 55 PRINT I to display the counter. If I never prints, the FOR line was never reached.