Perl while 循环

Perl 教程 · 2019-02-26 21:12:25

while 语句在给定条件为 true 时,重复执行语句或语句组。循环主体执行之前会先测试条件。

语法

语法格式如下所示:

while(condition)
{
   statement(s);
}

在这里,statement(s) 可以是一个单独的语句,也可以是几个语句组成的代码块。

condition 可以是任意的表达式,当条件为 true 时执行循环。 当条件为 false 时,程序流将退出循环。

流程图

Perl 中的 while 循环

图表中,while 循环的关键点是循环可能一次都不会执行。当条件为 false 时,会跳过循环主体,直接执行紧接着 while 循环的下一条语句。

实例

#!/usr/bin/perl $a = 10; # 执行 while 循环 while( $a < 20 ){ printf "a 的值为 : $a\n"; $a = $a + 1; }

程序中在变量 $a 小于 20 时执行循环体,在变量 $a 大于等于 20 时,退出循环。

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

a 的值为 : 10
a 的值为 : 11
a 的值为 : 12
a 的值为 : 13
a 的值为 : 14
a 的值为 : 15
a 的值为 : 16
a 的值为 : 17
a 的值为 : 18
a 的值为 : 19

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

查看所有标签

Computational Geometry

Computational Geometry

Mark de Berg、Otfried Cheong、Marc van Kreveld、Mark Overmars / Springer / 2008-4-16 / USD 49.95

This well-accepted introduction to computational geometry is a textbook for high-level undergraduate and low-level graduate courses. The focus is on algorithms and hence the book is well suited for st......一起来看看 《Computational Geometry》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具