Loop control statements control the execution of a loop in our code. They can change the execution sequence according to the programmer’s needs.
Perl includes the following loop control statements:
continue
goto
last
next
redo
continue
statementA continue
statement is executed just before the loop condition is re-evaluated.
Any code written within a continue
block is executed. Then the line counter is reset, and we jump back to the start of the loop.
We can use the continue
statement with while
and foreach
loops. The following is the syntax for writing a continue
block:
continue {
# statements to be executed just before the loop is reset again
}
$x = 0;while($x <= 5) {print "x = $x\n";} continue {$x++;}
In the example above, we write the increment statement ($x++
) within the continue
block.
As a result, x
is incremented just before the loop condition is evaluated again.
goto
statementA goto
statement shifts the program’s control flow from one place to another.
There are three forms of the goto
statement:
goto
- LABEL: Jumps to the statement identified with LABEL.
goto
- EXPR: Expects the expression (EXPR) to return a label and then jumps to that labeled statement.
goto
- &NAME: Substitutes a call to the named subroutine (&NAME) for the currently running subroutine.
The following is the syntax for a goto
statement:
goto LABEL / EXPR / &NAME
$x = 0;# goto labelLOOP: while ($x <= 10){if($x == 2 || $x == 3 || $x == 5 || $x == 7){print "$x is prime\n";$x++;goto LOOP}$x++;}print "--------------------------------------------\n";#goto expression$y = 0;$first_half = "PR";$second_half = "IME";PRIME: while ($y <= 10){if($y == 2 || $y == 3 || $y == 5 || $y == 7){print "$y is prime\n";$y++;goto $first_half.$second_half;}$y++;}print "--------------------------------------------\n";#goto subroutine$z = 0;while ($z <= 10){goto &isPrime($z);INC: $z++;}goto FINISH;sub isPrime{my $n = shift @_;if ($n > 10 || $n < 0){goto ERROR;}elsif ($n == 2 || $n == 3 || $n == 5 || $n == 7){print "$n is prime\n";}goto INC;}ERROR: print "ERROR: Input must be between 0 and 10\n";FINISH: print "\nWe learned goto!";
In the example above, we go through all forms of the goto
statement.
In the first section, we use goto
with the LOOP label.
The second section defines a new label, PRIME, which is passed to the goto
statement as an expression that concatenates two strings.
Finally, we define a subroutine and use goto
to jump from our loop to the subroutine and back.
last
statementThe last
statement immediately terminates the loop. Once the last
statement is executed, we exit from the scope of the loop, and the next statement after the loop is executed.
If a continue
block has been defined, it is not executed once the last
statement is encountered.
We can also pass an optional label (to a loop) to the last
statement. The last
statement applies to the nearest loop when used within a nested loop and without a label.
The following is the syntax for a last
statement:
last [LABEL]
$x = 0;while ($x <= 10){print "x = $x\n";if($x == 5){last;}$x++;}print "\nLoop ended!\n"
In the above example, the loop is set to run until x
is less than or equal to 10.
However, since we have added the last
statement for when x
is equal to 5, the loop terminates.
next
statementThe next
statement skips the remainder of the loop and starts the next iteration of the loop.
We can also use next
with a LABEL for a loop. If a continue
block is defined, it is always executed once a next
statement is encountered.
Below is the syntax of a next
statement:
next [LABEL]
@list = (2, 3, 4, 5, 6, 7);# foreach loop executionforeach $x (@list) {if ($x == 2 || $x == 3 || $x == 5 || $x == 7){print "$x is prime\n";next;}print "$x is not prime\n";}
Here, we define a list of numbers. If the number is prime, the relevant statement is printed and the next
statement takes us back to the start of the loop.
redo
statementThe redo
statement restarts the loop but does not evaluate the loop condition again.
The redo
statement can also be passed a label for a loop. If used within a nested loop and without a label, the redo
statement will be applicable to the nearest loop.
If a continue
block is defined, it is not executed.
The following is the syntax for a redo
statement:
redo [LABEL]
$x = 1;# Assigning label to loop$itr = 1;LOOP: while($itr < 10) {$x++;$itr++;redo LOOP if ($x < 100);}# Printing the valueprint ("x = $x\niterations: $itr\n");
Here, we define the LOOP label and associate it with the while
loop.
The while
loop has been set to accommodate 10 iterations. However, at the end of the loop, we include the redo
statement. This allows the loop to restart without checking the loop condition. Therefore, we can see that until x
acquires a value of 100, the loop will continue to function.
The total number of iterations is also 100, a lot more than our condition of 10.
Free Resources