Perl next Statement

Summary: in this tutorial, you will learn how to use the Perl next statement to control the flow of the loops.

Introduction to Perl next statement

The Perl next statement is used inside a loop to start the next iteration and skip all code below it.

In practice, you often use the next statement in conjunction with the if statement to specify a condition to start the next iteration.

Typically, you use the next statement in the while and for loop statement. For examples.

In a while loop statement:

while(condition){ next if(expression); # code to process the selected element }
Code language: Perl (perl)

In a for loop statement:

for(@array){ next if(expression); # code to process the selected element }
Code language: Perl (perl)

The next statement is like the continue statement in C.

The following flowchart illustrates how the Perl next is used in conjunction with an if statement inside a while loop.

Perl next with while statement

Perl next statement examples

You often use the next statement in the for, while and until loop statements.

For the do…while and do…until statements, you need to place a block for the next statement.

1) Perl next with the for statement example

Let’s take a look at the following example:

#!/usr/bin/perl use warnings; use strict; my @haystack = (1,4,3,2,5,6,8,7,9); # get an integer in the range 1-9 from command line my $needle = 0; do{ print "Enter a number to search(1-9):\n"; $needle = int(<STDIN>); }until($needle >= 1 && $needle <= 9); # find the position of the input integer my $pos = -1; for my $i (@haystack){ $pos++; next if($i != $needle); print("Found number $needle at position $pos\n"); }
Code language: Perl (perl)

How it works.

  • First, we declared an array of integers from 1 to 9.
  • Second, we asked users to input an integer within the range to search for. We used the do until loop to prompt for the input until the input integer is in the range.
  • Third, we looped over elements of the @haystack array to find the position of the input integer. Inside the loop, if the current element of the array is equal to the input integer, we displayed a message. We used the next statement to start the next iteration and skip the code that displays the message.

The following is the output of the program when we enter number 3 to search:

Enter a number to search(1-9): 3 Found number 3 at position 2
Code language: Perl (perl)

2) Perl next with while loop example

In the following example, the program asks you to enter 5 positive integers and push them to an array.

If you enter an invalid number, the next statement skips the code that increases the counter and pushes the number to the array.

After the loop, the program displays the content of the array that contains input numbers.

#!/usr/bin/perl use warnings; use strict; use constant MAX => 5; my @nums = (); my $num = 0; my $count = 0; print "Enter " . MAX . " positive integers:\n"; while($count < MAX){ $num = int(<STDIN>); # skip if the input number is not the positive integer next if($num <= 0); # push the positive integer to the array push(@nums,$num); $count++; } print("You entered: @nums\n");
Code language: Perl (perl)

The output of the program:

Enter 5 positive integers: -1 1 0 2 3 7 0 1 You entered: 1 2 3 7 1

For more information on using the next statement with do while and do until statements, check it out the respective tutorial.

In this tutorial, you’ve learned hơ to use the next statement to start the next iteration of the loop.

Was this tutorial helpful ?