For Next

From BR Wiki
Revision as of 19:28, 9 February 2012 by Mikhail.zheleznov (talk | contribs)
Jump to navigation Jump to search
FOR counter = initial_value to final_value [STEP step_value]
   [loop body]
NEXT counter

A FOR LOOP allows you to specify that an application should repeat a statement or set of statements until a counter reaches some its final value.

The loop body may consist of zero or more statements. However, the use of at least one statement in the loop body is encouraged for clarity.

The optional numeric step_value following the STEP keyword specifies by how much to increase or decrease the counter variable with each execution of the FOR LOOP. If the STEP keyword is absent and step_value isn't specified, then the default value of +1 is used. If step_value is negative, then the counter variable is decreased with each execution of the loop.

Examples

This example prints integers from 1 to 100:

00010 for counter = 1 to 100
00020    print counter
00030 next counter

The same effect may be achieved by this program:

00010 for counter = 1 to 100 step 1 ! this is redundant, since the default value of 1 is used anyway.
00020    print counter
00030 next counter

This program prints numbers from 100 to 1 in descending order:

00010 for counter = 100 to 1 step -1
00020    print counter
00030 next counter

This program prints EVEN numbers from 2 to 100 in ascending order:

00010 for counter = 2 to 100 step 2
00020    print counter
00030 next counter