Using the next statement in a foreach structure. : Next « Statement « Perl






Using the next statement in a foreach structure.

 

foreach ( 1 .. 10 ) {

   if ( $_ == 5 ) {
      $skipped = $_;  # store skipped value
      next;  # skip remaining code in loop only if $_ is 5
   }                    
        
   print "$_ ";
}

print "\n\nUsed 'next' to skip the value $skipped.\n";

   
  








Related examples in the same category

1.Next statement
2.Next if
3.Going onto the Next
4.A word-counting program that uses the next statement.
5.Using both next and continue
6.next with condition
7.Next statement with if statement