Perl continue Statement
The Perl continue block is typically executed before the condition is re-evaluated.
The continue statement can be used in while and foreach loops.
Syntax
The syntax for the continue statement in a while loop is as follows:
while(condition){
statement(s);
}continue{
statement(s);
}
The syntax for the continue statement in a foreach loop is as follows:
foreach $a (@listA){
statement(s);
}continue{
statement(s);
}
Example
Using the continue statement in a while loop:
Example
#/usr/bin/perl
$a = 0;
while($a < 3){
print "a = $a\n";
}continue{
$a = $a + 1;
}
Executing the above program will produce the following output:
a = 0
a = 1
a = 2
Using the continue statement in a foreach loop:
Example
#/usr/bin/perl
@list = (1, 2, 3, 4, 5);
foreach $a (@list){
print "a = $a\n";
}continue{
last if $a == 4;
}
Executing the above program will produce the following output:
a = 1
a = 2
a = 3
a = 4