Perl continue 语句
continue BLOCK,总是在条件即将被再次评估之前执行。 continue 语句可以与 while 和 foreach 循环一起使用。 continue 语句也可以与代码块一起单独使用,在这种情况下,它将被假定为流控制语句而不是函数。
语法
带有 while 循环的 continue 语句的语法如下 −
while(condition) {
statement(s);
} continue {
statement(s);
}
带有 foreach 循环的 continue 语句的语法如下 −
foreach $a (@listA) {
statement(s);
} continue {
statement(s);
}
带有代码块的 continue 语句的语法如下 −
continue {
statement(s);
}
示例
以下程序使用 while 循环模拟 for 循环 −
#/usr/local/bin/perl
$a = 0;
while($a < 3) {
print "Value of a = $a\n";
} continue {
$a = $a + 1;
}
这将产生以下结果 −
Value of a = 0 Value of a = 1 Value of a = 2
以下程序显示了 continue 语句与 foreach 循环的用法 −
#/usr/local/bin/perl
@list = (1, 2, 3, 4, 5);
foreach $a (@list) {
print "Value of a = $a\n";
} continue {
last if $a == 4;
}
这将产生以下结果 −
Value of a = 1 Value of a = 2 Value of a = 3 Value of a = 4

