A "for" loop is used mainly in looping through uniformly increasing or decreasing number series.
for (i from 1 to 10) display "She loves me {i|?:@value%2 == 0,not}<br>\n";
(Showing off a little at the end, but I believe you get the idea. If not, please consult the BEE Script User Reference - PDF document under Section "Conversion".)
We can also count down:
for (i from 10 to 1) display "{i}\n";
display "<br>We have a lift off!<br>\n";
The default "step" is 1 for increasing sequences and -1 for decreasing ones. However, you can explicitly specify the "step".
for (i from 3 to 11 step 2) display "{i}\n";
// Output: 3 5 7 9 11
for (i from 3 to 11 step 3) display "{i}\n";
// Output: 3 6 9
for (i from 15 to 8 step -4) display "{i}\n";
// Output: 15 11
for (i from 1 to -5 step -2) display "{i}\n";
// Output: 1 -1 -3 -5
The loop variable (i in the above case) runs through the range inclusively. For example, in the first example above, the last number is 11, the higher range boundary. In the second "for" above, the last number is 9, because the next number in the sequence would bring the variable to 12, which is beyond the higher range boundary.
If "step" is explicitly specified but in the "wrong" direction, the loop will not be executed. The following loops will not be executed:
for (i from 1 to 5 step -2) display "{i}\n";
for (i from 7 to 2 step 3) display "{i}\n";
|