Perl continue Statement
A Perl continue block is usually executed before the conditional statement is evaluated again.
The continue statement can be used in while and foreach loops.
Syntax
The syntax of the continue statement in a while loop is as follows:
while(condition){
statement(s);
}continue{
statement(s);
}
The syntax of 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
;
}
Running the above program, the output is:
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
)
{
"
a =
$a
\n
"
;
}
continue
{
last
if
$a
==
4
;
}
Running the above program, the output is:
a = 1
a = 2
a = 3
a = 4
Perl Loops