While
- while (test_expr)
{
statement(s); # Executed while test_expr is true
}
- Both the {} and the () are required!
- The test expression is evaluated and, if true, the statement block
is executed. This continues until the test expression is false.
- Note that the loop body may never be executed
- A compound statement
- Ex.
$i = 1;
while ($i <= 10)
{
print ("The square of $i is ", $i*$i, "\n");
$i++;
}
Until
- until (test_expr)
{
statement(s); # Executed while test_expr is false
# (Executed until test_expr is true)
}
- The test expression is evaluated and, if false, the statement block
is executed. This continues until the test expression is true.
- Note that the loop body may never be executed
- A compound statement
- Ex.
$i = 1;
until ($i > 10)
{
print ("The square of $i is ", $i*$i, "\n");
$i++;
}
Do Operator Applied To A Block
- do
{
statement(s);
}
- The {} are required!
- The statement block is executed.
- Returns the value of the last statement executed in the statement
block
- Allows the use of a statement block where an expression is required
- Can be modified with a while or until to act as a loop
Do-While
- do
{
statement(s); # Executed while test_expr is true
} while (test_expr);
- Only the {} are required!
- The statement block is executed. Then the test expression is
evaluated and, if true, the statement block is executed. This
continues until the test expression is false.
- Note that the loop body is executed at least once
- A simple statement! (Really just a modified Do-BLOCK.)
- Since it is just a simple statement, the loop control commands
(last, next, redo) desscribed later can NOT be used in a
do-while
- Ex.
$i = 1;
do
{
print ("The square of $i is ", $i*$i, "\n");
$i++;
} while ($i <= 10);
Do-Until
- do
{
statement(s); # Executed while test_expr is false
} until (test_expr);
- Only the {} are required!
- The statement block is executed. Then the test expression is
evaluated and, if false, the statement block is executed. This
continues until the test expression is true.
- Note that the loop body is executed at least once
- A simple statement! (Really just a modified Do-BLOCK.)
- Since it is just a simple statement, the loop control commands
(last, next, redo) desscribed later can NOT be used in a
do-until
- Ex.
$i = 1;
do
{
print ("The square of $i is ", $i*$i, "\n");
$i++;
} until ($i > 10);
Expression Modifiers
- Can use while and until as expression modifiers
- Similar to the while and until statement except that only an
expression can be modified, NOT a statement block
- Result is a simple statement
While Modifier
- exec_expr while test_expr;
- Exec_expr executed while the test_expr is true
- Equivalent to: while (test_expr)
{
exec_expr;
}
- The () around the test expression are NOT required here!
- The test expression is evaluated first
- A simple statement
- Ex.
# Print 10 down to 1.
$x = 11;
print "$x\n" while (--$x > 0);
# An infinite loop!
print ("Possible infinite loop here!\n") while ($x < 21);
Until Modifier
- exec_expr until test_expr;
- Exec_expr executed until the test_expr is true
- Equivalent to: until (test_expr)
{
exec_expr;
}
- The () around the test expression are NOT required here!
- The test expression is evaluated first
- A simple statement
- Ex.
# Print 10 down to 1.
$x = 11;
print "$x\n" until (--$x == 0);
For
- for (initial_expr; test_expr; increment_expr)
{
statement(s);
}
- Both the {} and the () are required!
- The initial expression is evaluated first (and only once). Then
the test expression is evaluated and, if true, the statement block
is executed. Then the increment expression is evaluated and the
test expression re-evaluated. This continues until the test
expression is false.
- Note that the loop body may never be executed
- Equivalent to:
initial_expr;
while (test_expr)
{
statement(s);
increment_expr;
}
- A compound statement
- Ex.
for ($i = 1; $i <= 10; $i++)
{
print ("The square of $i is ", $i*$i, "\n");
}
Foreach
- foreach $var (@list)
{
statement(s);
}
- Both the {} and the () are required!
- The $var variable is assigned the first value in the list @list
and the statement block executed. This is repeated for each value
in the list.
- If $var is omitted, the special default variable, $_, is used
- Note that the loop body is not executed if the list is the empty
list
- A compound statement
- Ex.
foreach $i (1..10)
{
print ("The square of $i is ", $i*$i, "\n");
}
Array Element Modification With A Foreach
- If the list used in the foreach statement is a single array
variable, you can modify each element of the array by
modifying $var each time through the loop
- Why? Because in this case, $var is really a reference to
the array element and not a copy of it.
- Ex.
foreach $x (@list)
{
# Add a newline to each element.
$x .= "\n";
}
Labeled Block
- Block with an associated name or label
- Label is an identifier similar to a variable name, but without
any special prefix character
- Recommended that labels be all uppercase to avoid conflict with
reserved words
- Label goes immediately in front of the statement containing the
block followed by a colon
- Labels have their own namespace
- Ex.
LOOP: foreach $i (1..10)
{
print ("The square of $i is ", $i*$i, "\n");
}
Last Operator
- Breaks out of the innermost enclosing loop block if used without
a label, or the specified block if used with a label
- Similar to the C break statement
- If the specified loop contains a continue block, it is skipped
- Only the while, until, for and foreach statement is considered
a loop by the last operator
- BUT a block by itself (a block which is NOT part of a larger
construct, such as a while loop or if-else statement) is
considered a loop that executes once, and the last operator can
be used to exit the block. This type of block is called a
"naked" block.
- Ex.
while (test_expr)
{
statement(s);
if (expr)
{
statement(s);
last; # Break out of the while loop
}
statement(s);
}
Next Operator
- Causes the rest of the specified loop to be skipped
- Similar to the C continue statement
- If the specified loop contains a continue block, it is executed
and then the loop conditional is evaluated
- Only the while, until, for and foreach statement is considered
a loop by the next operator
- The next operator can be used to exit a naked block. (The
difference between the next operator and the last operator in
this case, is that the last operator skips any continue block,
the next operator executes it.)
- Ex.
while (test_expr)
{
statement(s);
if (expr)
{
statement(s);
next; # Start next iteration, evaluate
# test_expr first
}
statement(s);
}
Redo Operator
- Causes the rest of the specified loop to be skipped
- BUT the loop conditional is NOT evaluated prior to the start of
the next iteration
- If the specified loop contains a continue block, it is NOT
executed
- Only the while, until, for and foreach statement is considered
a loop by the redo operator
- The redo operator can be used to restart a naked block.
- Ex.
while (test_expr)
{
statement(s);
if (expr)
{
statement(s);
redo; # Start next iteration, do NOT evaluate
# test_expr first
}
statement(s);
}
Continue Block
- Both the while loop and the until loop can also have a
continue block
- If a continue block exists, it is always executed before
the loop conditional expression is evaluated again
- Ex.
while ($line = )
{
chop $line;
next if ($line eq "END");
# Other Processing here.
}
continue
{
print "$line\n";
}
The input line is always printed, even if the next statement
is executed, since the continue block is always executed
before the loop conditional is evaluated again.