How to use next and last in Perl

next skips to the next evaluation of the loop

When you use next in Perl, you skip to the next evaluation of the current loop. In other words, if the loop is nested within another control structure, next only affects the loop which contains it.

my $counter;

my $test_var;

 

 

for( $counter = 0; $counter < 3; $counter++)

{

   print "counter: $counter/n";

   foreach $test_var (1,2,3,1,2,3) # Point A

   {

      if( $test_var == 2 )

      {

         next; # Jumps to Point A

      }

      print "$test_var ";

   }

   print "/n";

}

 

 

print "DONE!";

 

 

OUTPUT:

 

 

counter: 0

1 3 1 3

counter: 1

1 3 1 3

counter: 2

1 3 1 3

DONE!

As you can see, we iterate three times. Within that loop, Perl prints the values for $test_var in the foreach loop. Normally that means it should print 1 2 3 1 2 3. However, when $test_var is equal to two, Perl hits the next statement and skips to the loop at Point A, and thus it never gets printed. The foreach loop continues as it normally would, so the remaining values get printed.

If you need to have Perl jump to a specific control structure with next, you can label it. Let's say that when Perl hits the next statement, we'd like it to jump to the for loop, which we'll call Point B. Here's how we'd modify the Perl code.

POINT_B: for( $counter = 0; $counter < 3; $counter++)

{

   print "counter: $counter/n";

   foreach $test_var (1,2,3,1,2,3) # Point A

   {

      if( $test_var == 2 )

      {

         next POINT_B;

      }

      print "$test_var ";

   }

   print "/n";

}

 

 

OUTPUT:

 

 

counter: 0

1 counter: 1

1 counter: 2

1

Now every time Perl hits next in the foreach loop, it jumps all the way out to the for loop, which is now labeled POINT_B. Both the print statements and the newline don't get printed, and that's why the results look so different.

Use last to break out of the loop entirely

Unlike next, Perl does no further evaluation when you use last. Instead Perl jumps to the first executable line of code outside of the loop. The syntax looks the same as it does for next. However, the results are quite different.

for( $counter = 0; $counter < 3; $counter++)

{

   print "counter: $counter/n";

   foreach $test_var (1,2,3,1,2,3) # Point A

   {

      if( $test_var == 2 )

      {

         last;

      }

      print "$test_var ";

   }

   print "/n";

}

 

 

OUTPUT:

 

 

counter: 0

1

counter: 1

1

counter: 2

1

DONE!

It's similar to using next with labels, as in the example above, but this time Perl prints the newline (/n). Why? Because it's the next line of code that follows foreach. In the example with next, Perl jumped to the label POINT_B without executing any more code, so the newline was skipped. This time Perl just quit the foreach loop, and continued with the rest of the code as it normally would. You can also use labels with last, but I'll leave that as an exercise for you to do.

while loop and continue in Perl

There is another form of the while loop in Perl, and this one has a continue block on it. A continue block is a block of code that is executed after the loop is finished, but before the loop is evaluated for the next iteration.

my $test = 0;

$counter = 0;
while($counter++ < 5) # Point A
{
   if($counter == 3)
   {
      $test = 1;
      next;
   }
   print "While Loop: $counter/n";
}

continue # Point B
{
   if($test == 1)
   {
      print "CONTINUE: $counter /n";
      $test = 0;
   }
}

print "DONE!"; # Point C

When Perl finishes the code within the block, it proceeds to the continue block at Point B, rather than returning directly to Point A as it does in the examples above. There's a bit of a twist in the example, because I've introduced the keyword next, which will be discussed in detail later in this feature. However, a quick explanation is that next is used to break out of a loop.

######################################################################

附注:

C++ Syntax: continue

Description

continue may only be used inside a loop, such as a for statement. It terminates the current iteration of the loop and proceeds directly to the next. In the case of a for loop it jumps to its increment-expr.

Where loop statements are nested, continue terminates the current iteration of the innermost one loop containing it.

Description

continue may only be used inside a loop, such as a for statement. It terminates the current iteration of the loop and proceeds directly to the next. In the case of a for loop it jumps to its increment-expr.

Where loop statements are nested, continue terminates the current iteration of the innermost one loop containing it.

Description

continue may only be used inside a loop, such as a for statement. It terminates the current iteration of the loop and proceeds directly to the next. In the case of a for loop it jumps to its increment-expr.

Where loop statements are nested, continue terminates the current iteration of the innermost one loop containing it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值