MC-Basic:FOR ... NEXT

From SoftMC-Wiki
Jump to: navigation, search

The FOR...NEXT loop repeats the statements enclosed in the loop a number of times, counting from a starting value to an ending value by incrementing or decrementing a loop counter. The loop continues to execute as long as the loop counter has not reached the ending value.

The counter variable initially has the value of the start value expression. The STEP value may be a floating point or integer value. The default STEP value is 1. After each repetition of the loop, the value of the counter is incremented or decremented by the value of the step size, and compared with the end value. At this point the loop is complete if:

- the loop is counting up and the counter is greater than the end value, or
        - the loop is counting down and the counter is less that the end value.

The start value, end value, and step size may be expressions. The end value and the step size expressions are evaluated each time they are referenced in the loop.

The counter argument to the NEXT keyword is optional. However, when FOR loops are nested, the counter for a nested loop must appear before the counter for any enclosing loop.

Syntax

FOR <counter> = <start value> TO <end value> {STEP <stepsize>}
       {<loopstatements>}

NEXT {<counter>}

Availability

All versions

Type

<start value>=Long, Double, or expression

<end value>=Long, Double, or expression

<stepsize>=Long, Double, or expression

Range

<stepsize> If not entered, it defaults to 1

<end value> The loop is complete when the counter value exceeds this value. For positive <stepsize>, this occurs when <counter> is greater than this value. For negative <stepsize>, this occurs when <counter> is less than this value

Scope

Task

Limitations

Write only. Do not use FOR…NEXT in Config.prg. The FOR…NEXT statements must be matched within each program block (PROGRAM..END PROGRAM, SUB..END SUB, and

ONEVENT..END ONEVENT).
Example              Program
       dim array1[5][5] as long
       dim I as long
                   For I = 1 to 5
                               Print "I="I                     'Prints 2, 3, 4, 5
                   Next I
                   For I=4 To 2 Step –0.5
                               Print "I="I                     'Prints 4.0, 3.5, 3.0, 2.5, 2.0
                   Next I

See Also