FOR..NEXT

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC

Description

FOR..NEXT provides a way to execute code a specific number of times. Use FOR..NEXT and a counter expression with a starting and ending value to loop until the counter condition is met. When the ending value of the counter is reached, or the next increment of the counter will be greater than the ending value, the code will exit the loop. However, the counter will still be incremented and may now hold a value greater than the number of loops completed.
Normally, the counter is incremented (added to) by one. But, it can also be decremented (subtracted from). And, the value is not limited to one, or even to integers or positive numbers. See Hints and Examples.
Use of the variable name that holds the counter value is optional in the NEXT statement, but if used, must match the name used in the FOR statement. (e.g. for idx...next idx)
See also WHILE..WEND and DO..LOOP for other loop constructs.

Syntax

  1. for..next
    for count = startValue to endValue [step stepValue]
    [code statements]
    next [count]

Hints

You may increment, or decrement, the counter by a decimal value, .01 for instance, or by a negative value, -2 as an example.
Be aware that non-integer values are approximated by the computer, so you can end up one step short of the desired end value (see example). If you need a precise number of steps, use an integer counter and calculate your needed value (see example).
To increment (or decrement) by a value other than one (1), use the STEP modifier. Specify the value you want the "step" to be. (e.g. for i = 1 to 20 step 4)
To increment the counter starting with a larger number than the ending number, you must use STEP -(an amount).
Normally, the loop will execute the specific number of times. Do not issue a GOTO statement to exit a FOR..NEXT loop before the count has completed. It could corrupt JB stack, leading to unexpected results. If it is necessary to exit a loop before it has finished, issue an EXIT FOR command.
However, you may branch from a FOR..NEXT loop using GOSUB, FUNCTION and SUB. This is because when any of these procedures finishes, program execution is returned to the statement within the loop following the call to the procedure. The NEXT statement will be encountered and the FOR statement will once again evaluate the counter expression.
It happens that you cannot use global variables in a sub/function as loop counters (actually, if you do that, there must be a problem in your program logic, but still, results are unexpected).
endValue and stepValue get read only once on the loop start. Any changes while in the loop will not take effect (: variables/expressions could change, but loop will continue with old parameters).
This means that there no reason to "cache" expressions used for endValue and stepValue: they'll evaluate only once.

Example

' The basic use of FOR..NEXT
    for i = 1 to 10
        print i
    next i

    print

' Basic use without var after next
    for i = 1 to 10
        print i
    next

    print

' Using step +
    for i = .1 to 1 step .1
        print i
    next i

    print

' Using step -
    for i = 10 to 1 step -1
        print i
    next i

    print

'example where FOR ends one step less then expected (1.9 vs. 2.0)
'due to the fact that non-integer values are stored as approximations
    for i = 1 to 2 step .1
        print i
    next i

'A way around the "one step less" problem is to use an integer counter
'and calculate the needed value from startValue, stepValue and step count (integer counter)
    startValue=1 : stepValue = 0.1
    for i = 0 to 10
        x=startValue+i*stepValue
        print x
    next i

' Using EXIT FOR
    for i = 1 to 10
        if i > 5 then exit for
        print i
    next i
    'wait

' Showing counter increments after loop is complete
    for i = 1 to 10
        print i
    next i
    print "The value of i is now "; i

' Adjusting for additional counter increment
    for i = 1 to 10
        print i
    next i
    totItems = i -1
    print "Number of items is "; totItems

    end

Useful Procedures

' Place a useful function or sub using this keyword here