zjzc01:/root/test# cat a3.pl sub mask { my $n=shift; my $j =100; for ($i = 1;$i <= 5;$i++){ print "\$i is $i\n"; while ($n=$n+1) { $j++; print "\$n is $n\n"; if ($n==10){print "\$n---end is $n\n";$n=0;last}; }} }; &mask(1); zjzc01:/root/test# perl a3.pl $i is 1 $n is 2 $n is 3 $n is 4 $n is 5 $n is 6 $n is 7 $n is 8 $n is 9 $n is 10 $n---end is 10 $i is 2 $n is 1 $n is 2 $n is 3 $n is 4 $n is 5 $n is 6 $n is 7 $n is 8 $n is 9 $n is 10 $n---end is 10 $i is 3 $n is 1 $n is 2 $n is 3 $n is 4 $n is 5 $n is 6 $n is 7 $n is 8 $n is 9 $n is 10 $n---end is 10 $i is 4 $n is 1 $n is 2 $n is 3 $n is 4 $n is 5 $n is 6 $n is 7 $n is 8 $n is 9 $n is 10 $n---end is 10 $i is 5 $n is 1 $n is 2 $n is 3 $n is 4 $n is 5 $n is 6 $n is 7 $n is 8 $n is 9 $n is 10 $n---end is 10 last 是退出当前循环 zjzc01:/root/test# cat a3.pl sub mask { my $n=shift; my $j =100; for ($i = 1;$i <= 5;$i++){ print "\$i is $i\n"; while ($n=$n+1) { $j++; print "\$n is $n\n"; if ($n==10){print "\$n---end is $n\n";$n=0;return}; }} }; &mask(1); zjzc01:/root/test# perl a3.pl $i is 1 $n is 2 $n is 3 $n is 4 $n is 5 $n is 6 $n is 7 $n is 8 $n is 9 $n is 10 $n---end is 10 return是退出当前函数 退出循环是break return是退出函数并返回, 如果函数有返回值,可以将返回值返回
perl return和break
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
break,continue,return区别
不做过多解释,直接看代码和执行结果continue代码如下:break代码如下:return代码如下:
break continue return -
break、continue和return语句的区别
break:中断执行并输出结果continue:跳过本次继续执行剩余代码return:退出循环中断执行,且返回函数形参
break continue return