Perl 中的 continue 语句函数

首页 / Perl入门教程 / Perl 中的 continue 语句函数

可以在 whileforeach 循环中使用continue语句。

continue - 语法

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

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

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

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

具有代码块的 continue 语句的语法如下-

continue {
   statement(s);
}

continue - 示例

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Go语言从入门到实战 -〔蔡超〕

研发效率破局之道 -〔葛俊〕

分布式协议与算法实战 -〔韩健〕

OAuth 2.0实战课 -〔王新栋〕

Selenium自动化测试实战 -〔郭宏志〕

编程高手必学的内存知识 -〔海纳〕

React Native 新架构实战课 -〔蒋宏伟〕

Dubbo源码剖析与实战 -〔何辉〕

云时代的JVM原理与实战 -〔康杨〕

好记忆不如烂笔头。留下您的足迹吧 :)