FOR


Language statement FOR


10
20  FOR expr
30
Defines a FOR/NEXT loop. FOR counter = start TO end [STEP incr] … NEXT. FOR element IN array (or map) … NEXT.


See examples


If there is insufficient information on this page and you wish learn more about FOR, please send an email to smallbasic@gmail.com. You can help to improve information about FOR by submitting an article using the comments link below. Note, an offline language reference text file is now avialable – see the Download section.

Begins the definition of a FOR/NEXT loop.

  • counter – A numeric variable to be used as the loop counter.
  • start – A numeric expression; the starting value of counter.
  • end – A numeric expression; the ending value of counter.
  • incr – A numeric expression; the value by which counter is incremented or decremented with each iteration of the loop. The default value is +1.

SmallBasic begins processing of the FOR/NEXT block by setting counter equal to start. Then, if ‘incr’ is positive and counter is not greater than end, the commands between the FOR and the NEXT are executed.

When the NEXT is encountered, counter is increased by ‘incr’, and the process is repeated. Execution passes to the command following the NEXT if counter is greater than end.

If increment is negative, execution of the FOR/NEXT loop is terminated whenever counter becomes less than end.

FOR/NEXT loops may be nested to any level of complexity, but there must be a NEXT for each FOR.

FOR C=1 TO 9
    PRINT C
NEXT

Statement FOR element IN array … NEXT

Begins the definition of a FOR/NEXT loop.

  • element – A variable to be used as the copy of the current element.
  • array – An array expression

The commands-block will repeated for LEN times. Each time the ‘element’ will holds the value of the current element of the array.

FOR/NEXT loops may be nested to any level of complexity, but there must be a NEXT for each FOR.

A=[1,2,3]
FOR E IN A
    PRINT E
NEXT
...
' This is the same with that
A=[1,2,3]
FOR I=LBOUND(A) TO UBOUND(A)
    E=A(I)
    PRINT E
NEXT