Perl provides 3 control statements which can be used inside a loop to override its normal behavior. NEXT, LAST, and REDO.
NEXT can be used inside a loop to skip the rest of the statment block during a specific occurance then continue until normal loop termination.
See it in action!
When the value of $counter equalled 20, the NEXT command took the program control back to the start of the loop, ignoring the remainder of the statement block. The print function thus did not output the line "counter has a value of 20 which is less than 25.".
LAST is similar to NEXT except it terminates the loop upon execution instead of returning to the start of the loop.
See it in action!
Once the counter value equalled 20, the loop was terminated.
REDO repeats a loop beyond its normal behavoir.
See it in action!
The last line output has 26 in the value which is obviously not less than 25. This example is a simple print output. The redo forced the loop to happen once more, the value was increased by one, and the print command did it's simple purpose.
A nested loop refers to a loop inside of another loop. This type of programming is allowed, but be cautious... make sure each loop has it's own ending and purpose. Just like nested tables or regular html tags themselves, overlapping and non-endings can create a problem web page.
See it in action!
The inside loop (counter2) is being executed during each single outside loop (counter1).
NEXT can be used inside a loop to skip the rest of the statment block during a specific occurance then continue until normal loop termination.
|
$counter = 15; while ($counter < 25) { $counter++; next if ($counter == 20); print "counter has a value of $counter which is less than 25. <br>"; } print "end of script."; |
When the value of $counter equalled 20, the NEXT command took the program control back to the start of the loop, ignoring the remainder of the statement block. The print function thus did not output the line "counter has a value of 20 which is less than 25.".
LAST is similar to NEXT except it terminates the loop upon execution instead of returning to the start of the loop.
|
$counter = 15; while ($counter < 25) { $counter++; last if ($counter == 20); print "counter has a value of $counter which is less than 25. <br>"; } print "end of script."; |
Once the counter value equalled 20, the loop was terminated.
REDO repeats a loop beyond its normal behavoir.
|
$counter = 15; while ($counter < 25) { $counter++; print "counter has a value of $counter which is less than 25. <br>"; redo if ($counter == 25); } print "end of script."; |
The last line output has 26 in the value which is obviously not less than 25. This example is a simple print output. The redo forced the loop to happen once more, the value was increased by one, and the print command did it's simple purpose.
A nested loop refers to a loop inside of another loop. This type of programming is allowed, but be cautious... make sure each loop has it's own ending and purpose. Just like nested tables or regular html tags themselves, overlapping and non-endings can create a problem web page.
|
$counter1 = 0; while ($counter1 < 5) { $counter1++; print "The value for counter1 is $counter1. <br>"; $counter2 = 1; while ($counter2 < 5) { $result = $counter1 * $counter2; print "$counter multiply by $counter2 equals $result. <br>"; $counter2++; } } print "end of script."; |
The inside loop (counter2) is being executed during each single outside loop (counter1).

