Perl 循环嵌套

Perl 教程 · 2019-02-26 22:28:21

Perl 语言允许在一个循环内使用另一个循环,下面演示几个实例来说明这个概念。

语法

嵌套 for 循环语句的语法:

for ( init; condition; increment ){ for ( init; condition; increment ){ statement(s); } statement(s); }

嵌套 while 循环语句的语法:

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

嵌套 do...while 循环语句的语法:

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

嵌套 until 循环语句的语法:

until(condition){ until(condition){ statement(s); } statement(s); }

嵌套 foreach 循环语句的语法:

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

实例

#!/usr/bin/perl $a = 0; $b = 0; # 外部循环 while($a < 3){ $b = 0; # 内部循环 while( $b < 3 ){ print "a = $a, b = $b\n"; $b = $b + 1; } $a = $a + 1; print "a = $a\n\n"; }

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

a = 0, b = 0
a = 0, b = 1
a = 0, b = 2
a = 1

a = 1, b = 0
a = 1, b = 1
a = 1, b = 2
a = 2

a = 2, b = 0
a = 2, b = 1
a = 2, b = 2
a = 3

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

查看所有标签

Machine Learning in Action

Machine Learning in Action

Peter Harrington / Manning Publications / 2012-4-19 / GBP 29.99

It's been said that data is the new "dirt"—the raw material from which and on which you build the structures of the modern world. And like dirt, data can seem like a limitless, undifferentiated mass. ......一起来看看 《Machine Learning in Action》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

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

在线压缩/解压 CSS 代码

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

在线XML、JSON转换工具