Name:
for
Syntax:

FOR variable = start TO end
  {code}
NEXT {variable}

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

FOR variable = start DOWNTO end
  {code}
NEXT {variable}

FOR variable = start DOWNTO end STEP decrement
  {code}
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.

Description:

For...next loops are used to repeat a section of code a number of times with a control variable which has a differing value each time through the loop. The 'start' and 'end' values specify what the control variable's values will be and how many times the loop is executed.

Every time the 'next' line is reached the value of variable is incremented (or decremented) by the step value (+1 by default). When the control variable is outside the start to end values the looping stops and program flow continues from the line after the next command.

For...next loops can be nested 8 deep (remember to use a different variable for each loop). The for...next loop can be prematurely ended by use of the exit command.

Note that a 'for...next' command will always be executed at least once, regardless of start and end values. Whether to repeat the code within the 'for...next' 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 'for...to..next' loop implementation is equivalent to, and a more readable and compact form of -

  LET variable = start
DO
   {code}
   LET variable = variable +/- increment
LOOP WHILE variable >= start AND variable <= end

 

FOR variable = start TO end

The control variable will initially be set to the 'start' value and increment by one up to the 'end' value. For example -

  FOR b0 = 1 TO 5
  The 'for...next' will repeat 5 times with control variable b0 having values 1, 2, 3, 4 and 5.

When a byte variable is used, the loop can be repeated up to 256 times, from a start value of 0 to an end value of 255.

Note that when a word value is used it is not possible to use the range '0 to 65535' or '$0000 to $FFFF' as when the control variable's value of 65535 ($FFFF) is incremented it will overflow to zero which is not outside the range specified so the for...next will repeat forever.

 

FOR variable = start TO end STEP increment

The control variable will initially be set to the 'start' value and increment until it is greater than the 'end' value. For example -

  FOR w1 = 3 TO 9 STEP 3
  The 'for...next' will repeat 3 times with control variable w1 having values 3, 6 and 9.

 

FOR variable = start TO end STEP -decrement

The control variable will initially be set to the 'start' value and decrement until it is less than the 'end' value. The start value should be greater than the end value. For example -

  FOR b7 = 6 TO 1 STEP -2
  The 'for...next' will repeat 3 times with control variable b7 having values 6, 4 and 2.

 

FOR variable = start DOWNTO end

The control variable will initially be set to the 'start' value and decrement until it is less than the 'end' value. The start value should be greater than the end value. For example -

  FOR b7 = 6 DOWNTO 3
  The 'for...next' will repeat 4 times with control variable b7 having values 6, 5, 4 and 3.

 

FOR variable = start DOWNTO end STEP decrement

The control variable will initially be set to the 'start' value and decrement by the step value specified until it is less than the 'end' value. The start value should be greater than the end value. For example -

  FOR b7 = 6 DOWNTO 1 STEP 2
  The 'for...next' will repeat 3 times with control variable b7 having values 6, 4 and 2.

 

NEXT {variable}

The 'next' command indicates the end of the 'for...next' loop and tests the control variable's value to see if the code within the 'for...next' 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 'for...next' command will always be executed at least once, regardless of start and end values. Whether to repeat the code within the 'for...next' 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 'for...next' 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 'for...next' loop can affect how many times the loop will execute. This may cause the 'for...next' to repeat indefinitely or more times than expected.

 

EXIT

An exit command will cause the 'for...next' loop to immediately terminate and program execution to continue after the 'next' command of the 'for...next' loop it is within.

Applies To:
All
See Also:
Related Create:
    Share:
    Print:

    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:
    main:	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.

    Comments

    • Login to leave a comment.