Updated April 4, 2023
Introduction to Perl next Statement
Perl next statement is working the same as continue statement in other languages, this statement is used to move the next iteration of our array or hash skipping all the elements of an array. The next statement is used to start the next statement or iteration of the loop, we can also provide a label with the next statement. Next statement in Perl is used inside the nested loop where it will apply to the nearest loop of the program code when we did not specify a label in next statement. It is very important to move to the next element.
Syntax:
1. Next;
2. Next [ label ];
Parameters
Below is the parameter description of syntax are as follows:
- Next: A next statement is used to execute the next statement of a block from the program code. The next statement is very useful and important to execute the next statement block of the code. A next statement is used inside the nested loop where the loop is useful.
- Label: This is an optional parameter of the next statement in perl. The label is defined as we have used the label as a label for next loop in Perl. We can consider if the label is inside the square braces the label is optional and if we have not specified any label in the statement then the next statement in perl will directly go to the next iteration of the nearest loop.
Flowchart
Below is the flowchart of the next statement in perl is as follows. The flowchart is nothing but a step by step execution of the program code. The figure shows flowchart of next statement in perl.
- Flowchart is very important to find the step by step execution of next statement that how it is works in the program statement.
- The above figure shows the pictorial representation or flow of next statement in program code. How next statement is working its shows in a flowchart.
- Flowchart of next statement starts with the keyword name as start and end with the name as stop or we can also end the flowchart with name as end keyword.
- After starting, the flowchart compiler will execute the block of the program code. If our blocks contains next statement then the execution will goto execute the next statement block of code or statement.
- Then the compiler will execute the condition statement. If the condition of the block is true then the compiler will execute the last statement of the program code.
- If the condition of the block is false then the compiler will again go into the loop. It will again execute the previous statement. It will execute the loop statement until the condition is not becoming true.
- If the block condition is true then the compiler will execute the last block of the program code.
- The flowchart is very important to display the pictorial flow of any program or code in Perl language.
How next Statement Works in Perl?
Below is the working of next statement in perl is as follows.
1. It is inside the loop for starting the next iteration and skipping all the code which was below the loop iteration. We can use the label with the next statement, there are multiple uses of label statements with the next statement in perl.
2. The next statement is used to start next statement or iteration of the loop, we can also provide a label with the next statement. It is very important to move to the next element.
3. It is working the same as continue statement in other languages, this statement is used to move the next element of our array or hash skipping all the elements of an array.
4. The next statement in perl is used inside the nested loop where it will apply to the nearest loop of the program code when we have not specified a label in the next statement. We can use an array element with the next statement to process and we can skip the rest of the element from an array using the next statement in perl.
5. If our condition is true then the array element will get skipped from an array using the next statement in perl. A next statement is used to execute the next statement of a block from program code. We can use next statement with conjunction to specify start the next iteration or statement.
6. We can commonly use next statement in for and while loop statements in perl. Next statement in perl basically skips the current execution of statement and executes the next iteration of code. We can use label with next statement. But label is optional parameter in next statement.
Examples
Below are the examples of next statement in perl.
Example #1
Perl next statement without using label. We have not using any label in below example.
Code:
#define array of number from 1 to 100
@array_number = (1..100);
##Display number from 1 to 100.
print("@array_number\n");
## Used for loop to print number from 1 to 100.
for ($number = 0; $number < @array_number; $number++) {
if ($number == 0 || $number == 20 || $number == 40 || $number == 60 || $number == 80 || $number == 99) {
next; ## Skip all the number only print 1, 21, 41, 61, 81 and 100
}
$array_number[$number] = "-";
}
## Print the number
print("@array_number\n");
Output:
Example #2
Perl next statement using label. We have using label in below example.
Code:
## Using label with next statement.
OUTER: for ($p = 0; $p < 5; $p++) { ## Used for loop to define value of p and q variables.
for ($q = 0; $q < 5; $q++) { ## Inner for loop for auto increment of p and q variables.
print("Label_inner: $p, $q\n");
if ($q == 1) {
next OUTER; ## Using outer label with next statement.
}
}
print("outer: $p, $q\n\n"); ## print the value of p and q variables.
}
Output:
Conclusion
The next statement in perl is very important and useful to move the cursor to the next iteration. The next statement in perl is used inside the nested loop where it will apply to the nearest loop of the program code when we have not specified a label in the next statement.
Recommended Articles
We hope that this EDUCBA information on “Perl next” was beneficial to you. You can view EDUCBA’s recommended articles for more information.