while, for, next and last

while and until

while repeats a block as long as the condition is true. until is the same loop with the test reversed, which is handy for waiting on something that should eventually become true.

my $tries = 0; while ($tries < 3) { $tries++; print "try $tries\n"; } my $waited = 0; until ($waited >= 2) { $waited++ } print "waited $waited\n"; # waited 2

perl demo.pl

try 1
try 2
try 3
waited 2

A loop that never changes its condition never ends. When a script hangs, the first place to look is the variable in the while test: is anything inside the block able to move it?

The C-style for

The three-part for loop exists in Perl with the same meaning as in C: start, test, step. Use it when the counter itself matters, and foreach when you only need the elements.

for (my $i = 1; $i <= 3; $i++) { print "line $i\n"; }

perl demo.pl

line 1
line 2
line 3

Declaring the counter inside the parentheses keeps it out of the surrounding scope, which is one more reason to write for (my $i = ...) rather than a bare variable.

next, last, redo

Three keywords steer a loop from inside. next skips the rest of this pass, last leaves the loop entirely, and redo runs the same pass again without rechecking the condition.

for my $n (1..6) { next if $n % 2 == 0; # skip even numbers last if $n > 5; # stop after 5 print "$n "; } print "\n"; # 1 3 5

perl demo.pl

1 3 5

redo is rare and easy to misuse: it restarts the block without advancing the counter, so a loop that redoes without changing anything spins forever. Prefer a small inner loop when you need a retry.

Labels

Nested loops need a way to say which one to leave. Put a name and a colon before the outer loop, then use it with last or next.

OUTER: for my $row (1..3) { for my $col (1..3) { last OUTER if $row * $col == 4; print "$row$col "; } } print "\n";

perl demo.pl

11 12 13 21

Labels are written in capitals by convention, and only exist to be named by next, last or redo. The goto keyword exists too; treat it as a museum piece and restructure the code instead.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is the difference between while and until?

    Answer

    until is while with the test reversed, so it repeats while the condition is false

    Both test before the block; until simply carries the test the other way round, which reads better for a condition that must become true.

  2. What are the three parts of for (my $i = 1; $i <= 3; $i++)?

    Answer

    Start, test, step

    The same three-part loop as in C: initialise once, test before each pass, step after each pass.

  3. Why write for (my $i = 0; ...) instead of declaring $i before the loop?

    Answer

    The counter stays inside the loop and does not leak into the surrounding scope

    The my in the parentheses gives the counter the scope of the loop, so no later code can see or change it by accident.

  4. What does redo do?

    Answer

    It runs the same pass again without rechecking the condition or advancing the counter

    That is why a redo that changes nothing spins forever; a small inner loop is usually clearer.

  5. How do you leave an outer loop from inside an inner one?

    Answer

    Put a label before the outer loop and use last LABEL

    A label names the loop that last, next or redo should act on, and capitals are the convention for it.

  6. A script hangs in a while loop. Where do you look first?

    Answer

    At the variable in the test: something inside the block must be able to change it

    A condition that nothing inside the block moves stays true, and the loop never ends.

  7. What does this program print? my $i = 0; while ($i < 3) { $i++; print "$i " } print "\n";

    Answer

    1 2 3

    The counter is increased before the print, so the first line shows 1 and the loop stops when the test fails at 3.

  8. What does this program print? for (my $i = 1; $i <= 3; $i += 2) { print "$i " } print "\n";

    Answer

    1 3

    The step is 2, so the counter goes 1 then 3 and the next value is past the limit.

  9. What does this program print? for my $n (1..6) { next if $n % 2 == 0; last if $n > 5; print "$n "; } print "\n";

    Answer

    1 3 5

    Even numbers are skipped and the loop ends after 5, so the odd numbers up to 5 are printed.

  10. What does this program print? OUTER: for my $r (1..2) { for my $c (1..2) { last OUTER if $r * $c == 2; print "$r$c "; } } print "\n";

    Answer

    11

    The first pair prints 11, then the product reaches 2 and last OUTER leaves both loops at once.

Article author: Arthur Isaev