There is no limit on the depth of nesting of for...next statements.
If Var is omitted from the next statement, it is assumed to be the counter variable for the most recent for statement.
undone = 1 while undone undone = 0 for i = 1 to j - 1 if a$(i) > a$(i + 1) then_ swap a$(i), a$(i + 1) : undone = 1 next i wendIn the next example, the loop executes six times. The final value for the loop variable is set before the initial value is set. The program
j = 3 for j = 1 to j + 3 print j; nextproduces
1 2 3 4 5 6For...next loops may be nested, with one or more loops inside another loop. Each loop must have a unique variable as its counter. Each next statement must correspond with the most recently occurring for statement. If several nested loops share the same end point, one next statement may be used for all of them. The final example illustrates this. The program
dim a(6, 6) for j = 1 to 5 for k = 1 to 5 a(j, k) = j + k next k, jis equivalent to
dim a(6, 6) for j = 1 to 5 for k = 1 to 5 a(j, k) = j + k next k next jA next statement of the form
next Var1, Var2, ..., VarNis equivalent to the sequence of statements
next Var1 next Var2 . . . next VarN
from The Basmark QuickBASIC Programmer’s Manual by Lawrence Leinweber