Monday, December 05, 2005

 

Perl: WHILE/DO loops

A language must have some form of flow control to be something better than
a batch processor. The simplest form of flow control, beyond IF-ELSE is
the DO loop. The first construct is the WHILE / DO loop:

while ( EXPRESSION ) {
STATEMENTS;
}

Note that the DO is implied. If the condition is initially false, the body
of the loop will never execute. In the next form, the loop will execute at
least once.

do {
STATEMENTS;
} while (EXPRESSION);

All loops support the following two control statements:
last
Declare that this is the last statement in the loop; completely exit the
loop even if the condition is still true, ignoring all statements up to
the loop's closing brace.
next
Start a new iteration of the loop

# one way to count to 10
$number = 0;
while(1) {
$number++;
if ($number >= 10 ) {
last;
}
}


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?