Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1002141
  • 博文数量: 128
  • 博客积分: 10012
  • 博客等级: 上将
  • 技术积分: 2050
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 17:49
文章分类

全部博文(128)

文章存档

2011年(16)

2009年(57)

2008年(55)

分类:

2009-03-13 13:46:54


   if ( ) {
    
   }
   elsif ( ) {
    
   }
   ...
   else{
    
   }


   while ( ) {
    
   }

   until ( ) {
    
   }
,如
   for ($count=1; $count <= 5; $count++) {
     # statements inside the loop go here
   }
下面是在for循环中使用逗号操作符的例子:
   for ($line = , $count = 1; $count <= 3;   $line = , $count++) {
     print ($line);
   }
它等价于下列语句:
   $line = ;
   $count = 1;
   while ($count <= 3) {
     print ($line);
     $line = ;
     $count++;
   }

   foreach localvar (listexpr) {
     statement_block;
   }
例:
   foreach $word (@words) {
     if ($word eq "the") {
       print ("found the word 'the'\n");
     }
   }
注:
(1)此处的循环变量localvar是个局部变量,如果在此之前它已有值,则循环后仍恢复该值。
(2)在循环中改变局部变量,相应的数组变量也会改变,如:
   @list = (1, 2, 3, 4, 5);
   foreach $temp (@list) {
     if ($temp == 2) {
       $temp = 20;
     }
   }
此时@list已变成了(1, 20, 3, 4, 5)。

   do {
     statement_block
   } while_or_until (condexpr);
   do循环至少执行一次循环。

   退出循环为last,与C中的break作用相同;执行下一个循环为next,与C中的continue作用相同;PERL特有的一个命令是redo,其含义是重复此次循环,即循环变量不变,回到循环起始点,但要注意,redo命令在do循环中不起作用。



   语法为statement keyword condexpr。其中keyword可为if、unless、while或until,如:
     print ("This is zero.\n") if ($var == 0);
     print ("This is zero.\n") unless ($var != 0);
     print ("Not zero yet.\n") while ($var-- > 0);
     print ("Not zero yet.\n") until ($var-- == 0);
   虽然条件判断写在后面,但却是先执行的。
阅读(507) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~