NEXT
From QB64 Wiki
NEXT is used in a FOR counter loop to progress through the loop count or to RESUME on the NEXT code line after an error.
- FOR i = 1 TO 10
- .
- . 'loop code
- .
- NEXT i
- OR
- RESUME NEXT
- NEXT is required in a FOR loop or a "FOR without NEXT" error will occur.
- The FOR variable name is not required after NEXT.
- NEXT can be grouped with other NEXTs in nested FOR loops using colons like NEXT: NEXT
- NEXT can also end more than one nested FOR loop using comma separated variables like NEXT i, j
- NEXT increases the FOR loop count so the variable value AFTER the FOR loop will be one more count than the requested count.
- Also used in ON ERROR GOTO procedures after RESUME to return the program to the next code line only after an error occurs.
Example: Finding the FOR variable value AFTER a simple counter loop to 10.
FOR i = 1 TO 10 PRINT i; NEXT i PRINT "AFTER the LOOP, NEXT makes the value of i ="; i
1 2 3 4 5 6 7 8 9 10 AFTER the LOOP, NEXT makes the value of i = 11
Result: The last value of i = 11 although FOR only looped 10 times. Only use the count values while inside of the loop or compensate for this behavior in your code!
See also: