Perl Learning: 2.9. The while Control Structure

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

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...