FOR variable = start TO end
NEXT {variable}

FOR variable = start TO end STEP {-}increment
NEXT {variable}

FOR variable = start DOWNTO end
NEXT {variable}

FOR variable = start DOWNTO end STEP decrement
NEXT {variable}

Variable - will be used as the loop counter

Start - is the initial value of variable

End - is the finish value of variable

Increment - is an optional value which overrides the default counter value of +1. If Increment is preceded by a '-', it will be assumed that Start is greater than End, and therefore increment will be subtracted (rather than added) on each loop.


The 'next' command indicates the end of the '' loop and tests the control variable's value to see if the code within the '' loop should be repeated or whether execution should continue after the 'next' command.

The control variable name may optionally be placed after the 'next' command and if used must match the control variable named in the 'for' command.

A '' command will always be executed at least once, regardless of start and end values. Whether to repeat the code within the '' loop is determined upon execution of the 'next' command, not upon execution of the 'for' command as may be the case in some other programming languages.

The '' start and end values are evaluated every time the 'next command is executed so if the start or end values are specified by variables, altering those variables within the '' loop can affect how many times the loop will execute. This may cause the '' to repeat indefinitely or more times than expected.


Applies To:
See Also:
Related Create:

    Flash an LED

    Flash an LED once, twice, then up to twenty times before repeating again. The cycle can be restarted from a single flash by setting the input C.1 high

    Code Example:
    	for b0 = 1 to 20		; define loop for 20 times
    	  if pinC.1 = 1 then exit
    	  high B.1			; switch on output B.1
    	  pause 500			; wait 0.5 seconds
    	  low B.1			; switch off output B.1
    	  pause 500			; wait 0.5 seconds
    	next b0				; end of loop
    	pause 2000			; wait for 2 seconds
    	goto main			; loop back to start
    Copy Code Submit an Example

    Submit Your Own Code!

    You must be logged in to submit code examples. Login now.