Perl goto 语句

Perl 教程 · 2019-02-26 23:44:26

Perl 有三种 goto 形式:got LABLE,goto EXPR,和 goto &NAME:

序号goto 类型
1goto LABEL

找出标记为 LABEL 的语句并且从那里重新执行。

2goto EXPR

goto EXPR 形式只是 goto LABEL 的一般形式。它期待表达式生成一个标记名称,并跳到该标记处执行。

3goto &NAME

它把正 在运行着的子进程替换为一个已命名子进程的调用。

语法

语法格式如下所示:

goto LABEL

或

goto EXPR

或

goto &NAME

流程图

实例

以下两个实例在变量 $a 为 15 时跳出输出。

以下是一个常用的 goto 实例:

实例

#/usr/bin/perl $a = 10; LOOP:do { if( $a == 15){ # 跳过迭代 $a = $a + 1; # 使用 goto LABEL 形式 print "跳出输出 \n"; goto LOOP; print "这一句不会被执行 \n"; } print "a = $a\n"; $a = $a + 1; }while( $a < 20 );

执行以上程序,输出结果为:

a = 10
a = 11
a = 12
a = 13
a = 14
跳出输出 
a = 16
a = 17
a = 18
a = 19

以下实例使用了 goto EXPR形式。我们使用了两个字符串,并使用点号 (.) 来链接。

实例

$a = 10; $str1 = "LO"; $str2 = "OP"; LOOP:do { if( $a == 15){ # 跳过迭代 $a = $a + 1; # 使用 goto EXPR 形式 goto $str1.$str2; # 类似 goto LOOP } print "a = $a\n"; $a = $a + 1; }while( $a < 20 );

执行以上程序,输出结果为:

a = 10
a = 11
a = 12
a = 13
a = 14
a = 16
a = 17
a = 18
a = 19

点击查看所有 Perl 教程 文章: https://codercto.com/courses/l/19.html

查看所有标签

Algorithms to Live By

Algorithms to Live By

Brian Christian、Tom Griffiths / Henry Holt and Co. / 2016-4-19 / USD 30.00

A fascinating exploration of how insights from computer algorithms can be applied to our everyday lives, helping to solve common decision-making problems and illuminate the workings of the human mind ......一起来看看 《Algorithms to Live By》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具