Perl next 语句开始循环的下一个迭代。您可以提供带有 next 语句的LABEL,其中LABEL是循环的标签。 next 语句可以在嵌套循环中使用,如果未指定LABEL,则该语句将适用于最近的循环。
next [ LABEL ];
方括号内的LABEL表示LABEL是可选的,如果未指定LABEL,则next语句会将控件跳转到最近的循环的下一个迭代。
#!/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-
#!/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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)