Skip to main content

FOR...NEXT

Syntax

FOR counter = startvalue TO endvalue [STEP stepvalue]     [statement block] NEXT [counter] FOR counter = startvalue DOWNTO endvalue [STEP stepvalue]     [statement block] NEXT [counter]

Description

A FOR [...] NEXT loop initializes counter to startvalue, then executes the statement block's, incrementing counter by stepvalue until it reaches endvalue. If stepvalue is not explicitly given it will set to 1.

If the DOWNTO is used, then the counter is decremented by the stepvalue or 1 if none is specified.

Example

PRINT "counting from 3 to 0, with a step of -1"

FOR i = 3 DOWNTO 0 STEP 1

PRINT "i is "; i

NEXT i

Differences from other BASICs

  • PBASIC does not use DOWNTO, and must specify a negative step
  • PBASIC does not allow the variable in the NEXT statement (while this is not necessary it is good coding practice)

See also