📜  珀尔 |最后一个循环

📅  最后修改于: 2022-05-13 01:55:48.538000             🧑  作者: Mango

珀尔 |最后一个循环

last 关键字用于循环控制语句,该语句立即导致循环的当前迭代成为最后一个。如果给定一个标签,那么它来自标签的循环。

示例 1:

#!/usr/bin/perl
$sum = 0;
$a = 0;
$b = 0;
  
while(1) 
{
  
$sum = $a + $b;
$a = $a + 2;
  
# Condition to end the loop
if($sum > 10) 
{
    print "Sum = $sum\n";
    print "Exiting the loop\n";
    last;
} 
else
{
    $b = $b - 1;
}
}
print "Loop ended at Sum > 10\n";

输出:

Sum = 11
Exiting the loop
Loop ended at Sum > 10

示例 2:

#!/usr/local/bin/perl
  
$a = 1;
$sum = 0;
  
# Outer Loop
Label1: while($a < 16) 
{
   $b = 1;
     
   # Inner Loop
   Label2: while ($b < 8)
   {
      $sum = $sum + $b;
      if($a == 8) 
      {
         print "Sum is $sum";
           
         # terminate outer loop
         last Label1;
      }
      $b = $b * 2;
   }
   $a = $a * 2;
}

输出:

Sum is 22