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 and Data Structures

Algorithms and Data Structures

Kurt Mehlhorn、Peter Sanders / Springer / 2008-08-06 / USD 49.95

Algorithms are at the heart of every nontrivial computer application, and algorithmics is a modern and active area of computer science. Every computer scientist and every professional programmer shoul......一起来看看 《Algorithms and Data Structures》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码