Classic Computer Magazine Archive COMPUTE! ISSUE 52 / SEPTEMBER 1984 / PAGE 10

Incomplete NEXTs

I have a VIC-20 and I have a question about the NEXT statement. I've seen some programs that had a NEXT statement with nothing after the next. For example:

FOR A = 1TO10 : NEXT

Why doesn't it include the variable after the NEXT as in:

FOR A = 1TO10 : NEXT A

Kevin Biebor

The NEXT statement increments or completes a loop that was started by a FOR statement. If a variable is placed after the NEXT, that loop is incremented. In the following example, the B loop will be incremented (and completed) ten times each time the A loop is incremented.

FOR A = 1TO10
FOR B = 1TO10

If the NEXT statement is not followed by a variable name, the loop completed will be the one most recently started. In the following example, the NEXT will complete the B loop even though the A loop was the first one started.

FOR A = 1TO10
FOR B = lTO10

Nested loops (loops within other loops) should be written with care. If they're programmed incorrectly, one or more of the loops may not be completed. For instance, the B loop in the following example will never be completed.

FOR A = 1TO10
FOR B = 1TO10