Perl next 语句
Perl next 语句开始循环的下一次迭代。 您可以提供带有 next 语句的 LABEL,其中 LABEL 是循环的标签。 next 语句可以在嵌套循环中使用,如果未指定 LABEL,它将适用于最近的循环。
如果循环中有 continue 块,则它总是在即将评估条件之前执行。 您将在单独的章节中看到 continue 语句。
语法
Perl 中 next 语句的语法是 −
next [ LABEL ];
方括号内的 LABEL 表示 LABEL 是可选的,如果未指定 LABEL,则下一条语句将控制跳转到最近循环的下一次迭代。
流程图
示例
#!/usr/local/bin/perl
$a = 10;
while( $a < 20 ) {
if( $a == 15) {
# skip the iteration.
$a = $a + 1;
next;
}
print "value of a: $a\n";
$a = $a + 1;
}
当上面的代码被执行时,它会产生下面的结果 −
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19
让我们举一个例子,我们将使用 LABEL 和 next 语句 −
#!/usr/local/bin/perl
$a = 0;
OUTER: while( $a < 4 ) {
$b = 0;
print "value of a: $a\n";
INNER:while ( $b < 4) {
if( $a == 2) {
$a = $a + 1;
# jump to outer loop
next OUTER;
}
$b = $b + 1;
print "Value of b : $b\n";
}
print "\n";
$a = $a + 1;
}
当上面的代码被执行时,它会产生下面的结果 −
value of a : 0 Value of b : 1 Value of b : 2 Value of b : 3 Value of b : 4 value of a : 1 Value of b : 1 Value of b : 2 Value of b : 3 Value of b : 4 value of a : 2 value of a : 3 Value of b : 1 Value of b : 2 Value of b : 3 Value of b : 4

