Cycle FOR should be used to repeat a group of statements a specified number of times. A variable should be used as a counter. Upper limit for the counter variable is stecified after keyword TO. The group of statements should be terminated by keyword NEXT. In the example below, a group of statements is executed 10 times, and a counter variable I is changing from 1 to 10:
for i = 1 to 10 DrawText (10, 10 + i * 20, "i=" & i) nextCounter variable is increased by 1 with every iteration in the above example, and the cycle is performed 10 times for i=1, i=2, i=3, i=4, i=5, i=6, i=7, i=8, i=9 and i=10 If you want to iterate in reverse order, you should use keyword DOWNTO instead of to
for i = 10 downto 1 DrawText (10, 10 + i * 20, "i=" & i) nextCounter variable is decreased by 1 with every iteration in the above example, and the cycle is performed 10 times for i=10, i=9, i=8, i=7, i=6, i=5, i=4, i=3, i=2 and i=1
If you want an increment for your counter variable different than 1 or -1, you should use keyword STEP:
for i = 1 to 10 step 2 DrawText (10, 10 + i * 10, "i=" & i) nextCounter variable is increased by 2 with every iteration in the above example, and the cycle is performed 5 times for i=1, i=3, i=5, i=7 and i=9
You can also use keyword STEP together with DOWNTO, but then your step should be negative:
for i = 10 downto 1 step -2 DrawText (10, 10 + i * 10, "i=" & i) nextCounter variable is decreased by 2 with every iteration in the above example, and the cycle is performed 5 times for i=10, i=8, i=6, i=4 and i=2
See also: