Monday, December 05, 2005
Perl: FOR loops
The next control flow construct up the complexity line is the FOR loop. It
is nearly identical to the C for loop.
for ( INITIAL_EXPR ; COND_EXPR ; LOOP_EXPR ) {
STATEMENTS;
}
The loop is initialized by evaluating INITIAL_EXPR, iterates while
COND_EXPR is true, and evaluates LOOP_EXPR before beginning each
subsequent loop.
# an example countdown
for ( $i=10 ; $i>=0 ; $i-- ) {
print("$i\n");
}
The loop flow can be modified using next and last, as in the DO loop.
Unlike C, the braces around the block of STATEMETNTS are mandatory