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

查看所有标签

Learn Python 3 the Hard Way

Learn Python 3 the Hard Way

Zed A. Shaw / Addison / 2017-7-7 / USD 30.74

You Will Learn Python 3! Zed Shaw has perfected the world’s best system for learning Python 3. Follow it and you will succeed—just like the millions of beginners Zed has taught to date! You bring t......一起来看看 《Learn Python 3 the Hard Way》 这本书的介绍吧!

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

在线压缩/解压 CSS 代码

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

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器