当前位置: 首页 > 文档资料 > Perl 入门教程 >

continue statement

优质
小牛编辑
127浏览
2023-12-01

continue BLOCK,总是在条件即将再次评估之前执行。 continue语句可以与whileforeach循环一起使用。 continue语句也可以与BLOCK代码一起单独使用,在这种情况下,它将被假定为流控制语句而不是函数。

语法 (Syntax)

while循环的continue语句的语法如下 -

while(condition) {
   statement(s);
} continue {
   statement(s);
}

带有foreach循环的continue语句的语法如下 -

foreach $a (@listA) {
   statement(s);
} continue {
   statement(s);
}

带有BLOCK代码的continue语句的语法如下 -

continue {
   statement(s);
}

例子 (Example)

以下程序使用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

以下程序显示了使用foreach循环的continue语句的用法 -

#/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