User Tools

Site Tools


for

Synopsis:

for (once, expression, each) { block }
for var from firstval to lastval { block }
for var from firstval to lastval step stepval { block }
for var in (word list) { block }

Description (standard form):

FOR is a general purpose loop. It is modeled on the C for statement, and works in a very similar manner. Aside from the action, there are three parts to a FOR loop:

The once part is a block executed before the loop begins iterating. This is often used for initializing counters and other variables that will be used in the loop.

Before each loop iteration, the condition expression is checked. Most often, this is used to see if the counter has exceeded a certain limit. The condition may contain any expression legal in the IF command. Because of this, the loop does not necessarily have to iterate at all.

The each part is a block executed each time the loop exits and before the condition is tested. This is where you would increment a counter that gets checked by the condition statement.

It is imperative to note that the three sections are separated by commas and not semicolons; this is because once and each may contain semicolons if they contain multiple statements. This is the reverse of C. The first and third section are COMMANDS and the second section is an EXPRESSION.

Once, each, and block are optional and may be left blank.

Description (for/next form):

This second form of the command provides a FOR .. NEXT type loop that enumerates each of the integers between firstval and lastval. Each enumerated integer is assigned to var and the block is executed.

If you wish enumerate only some of the values in the range, you can specify a step, which says that each stepth value should be enumerated.

If firstval is greater than lastval then you must specify a step value and it must be negative. If you do not do this, then the loop will not execute.

The var variable should be considered read-only. Changing it will not affect how the range is enumerated.

This form is much cheaper than a standard for, and is cheaper than a fe over a jot range.

The CONTINUE and BREAK commands are honored in the action body. The CONTINUE command will behave as a “next” operation, and the BREAK command will abort the loop entirely.

Description (for/each form):

This third and final form is a FOR .. IN LIST type loop that iterates over each word in the word list, assiging each value in turn to the var and executing the block.

This form is cheaper than fe because it is optimized for iterating one word at a time, whereas fe is general-purpose.

The CONTINUE and BREAK commands are also honored in the same way as in the second form above.

Examples:

Form 1:

To display a warning message 3 times:

    for ( @ xx = 3, xx > 0, @ xx-- ) {
       echo WARNING!  This ship will self destruct in $xx seconds!
    }

A infinite loop that behaves like the Unix 'yes' command:

    for ( ,, ) {
       echo yes
    }

Form 2:

Display the numbers 1 through 5 each on a separate line:

    for ii from 1 to 5 {
       echo $ii
    }

Display the numbers 1, 3, and 5 each on a separate line:

    for ii from 1 to 5 step 2 {
       echo $ii
    }

Form 3:

Display the words “one”, “two”, and “three” each on a separate line:

    for xx in (one two three) {
       echo $xx
    }

History:

for.txt · Last modified: 2006/08/01 03:13 (external edit)