Perl Learning: 2.9. The while Control Structure

本文详细介绍了Perl编程语言中的while循环结构,包括其基本语法、如何避免无限循环以及循环内部的操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Previous Page
Next Page

 

2.9. The while Control Structure

Like most algorithmic programming languages, Perl has a number of looping structures.[] The while loop repeats a block of code as long as a condition is true:

[] Every programmer eventually creates an infinite loop by accident. If your program keeps running and running, you can generally stop it in the same way you'd stop any other program on your system. Often, typing Ctrl-C will stop a runaway program; check with your system's documentation to be sure.

    $count = 0;
    while ($count < 10) {
      $count += 2;
      print "count is now $count/n"; # Gives values 2 4 6 8 10
    }

As always in Perl, the truth value here works like the truth value in the if test. Like the if control structure, the block curly braces are required. The conditional expression is evaluated before the first iteration, so the loop may be skipped completely if the condition is initially false.

Previous Page
Next Page
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值