ForNext

=For...Next= toc The **For...Next** looping structure allows you to rerun a code routine the specified number of times. The syntax is as follows:

Syntax: FOR x = a TO b [STEP c] //- Loop while counting from a to b with optional counting step of c// NEXT [x] //- Specify end of FOR loop with optional variable identifier// EXIT FOR //- Quit executing code in the current FOR/NEXT loop and continue with the code following//

The counter variable x is incremented by one each time through the loop, unless the optional **STEP** is added. The code between the **For** statement and the **Next** statement is executed each time through the loop. **Next** increments the value of the counter variable and returns execution to the beginning of the loop. When the **To** value is reached, the loop terminates and program execution continues at the code line following the **Next** statement. The counter variable designation in the **Next** statement is optional.

Simple Loop
Here is a simple loop that prints out the values of i as i is incremented from 1 to 10. The **Next** statement does not include the name of the counter variable.

code format="vbnet" for i = 1 to 10 print "Value of i is ";i next code

Using Next
Here is the same code. This time the **Next** statement includes the name of the counter variable.

code format="vbnet" for i = 1 to 10 print "Value of i is ";i next i code

Here is a similar looping routine. **Next** again specifies the name of the counter variable. Loops needn't start counting at 1. This routine begins counting at 2.

code format="vbnet" for i = 2 to 27 print "Value of i is ";i next i code

Negative numbers can be used. The following code begins counting at -10. code format="vbnet" for i = -10 to 27 print "Value of i is ";i next i code

Using Step
If **Step** is specified, the counter variable is incremented by the value of **Step** each time, rather than being incremented by the default 1.

code format="vbnet" for i = 2 to 27 step 2 print "Value of i is ";i next i code

The value of **Step** can be a decimal, as well as a whole number. It can also be negative.

code format="vbnet" 'decimal value for steop for i = -10 to 27 step 0.5 print "Value of i is ";i next i code

code format="vbnet" 'negative value for step for i = 27 to -10 step -1 print "Value of i is ";i next i code

If the beginning value for the counter variable exceeds the ending value, the loop is not executed.

code format="vbnet" for i = 10 to 1 print "Value of i is ";i next code

Exit Loop Prematurely
If it is necessary to exit a loop prematurely, this can be done using the **Exit For** statement as follows.

code format="vbnet" for i = 1 to 100 j = i*3 if j>122 then exit for print "Value of i is ";i print "Value of j is ";j next code

In Liberty BASIC, we do not jump out of a loop before the final value of the counter variable is reached. **It is okay to jump out of a loop (for/next and while/wend) in Run BASIC.** Here is Carl's explanation:

//Yes, it is okay to jump out of a FOR NEXT or WHILE WEND loop in Run BASIC using GOTO. This works because of the new way we handle structure at runtime. In Liberty BASIC v4 all the executable objects are in a flat collection, and important stack manipulation is performed by the FOR and NEXT statements, so if you skip over the NEXT statement to get out of the loop then the stack gets messed up. In LB5 and Run BASIC each nested block of execution manages itself. If you GOTO outside of a loop the runtime looks for the branch label progressively to each next outer scope, throwing away each nested block (whether a loop, an IF THEN block, a SELECT CASE block, or whatever) until it finds the branch point. This automatically manages the execution context, and there is nothing else to clean up.//

Nested Loops
Loops may be nested. Here is an example with one loop inside of another loop. The **Next** statement includes the counter variable.

code format="vbnet" for i = i to 10 for j = 2 to 4 print "Value of i is ";i print "Value of j is ";j next j next i code

The example above properly indicated the counter variables in the **Next** statements, with the **i** variable on the outside and the **j** variable on the inside. The following example is **incorrectly nested.**

code format="vbnet" for i = i to 10 for j = 2 to 4 print "Value of i is ";i print "Value of j is ";j next i next j code

It is not necessary to specify the counter variable in the **Next** statement, even in nested loops. Run BASIC keeps track of the nesting. Here is the code again, without counter variables in the **Next** statements.

code format="vbnet" for i = i to 10 for j = 2 to 4 print "Value of i is ";i print "Value of j is ";j next next code

Arrays and Loops
Loops are most useful when used to operate on arrays. The index of the array is incremented in the loop. This simple example fills an array with strings in a loop. It then reads every second item in the array in another loop.

code format="vbnet" 'create an array of 100 strings Dim Items$(100)

'use a loop to fill the array for i = 1 to 100 Items$(i) = "Item";str$(i) next

'use a loop to access the array: for i = 2 to 100 step 2 print "Now reading ";Items$(i) next code