Perl Loops
A loop statement allows the execution of a statement or group of statements multiple times. There are five types of loop statement in Perl.
- ‘for’ loop – Useful when it is known in advance the number of times the statements within the loop need to be executed.
- 'foreach' loop - A variation of a 'for' loop that operates on a range or collection of values.
- ‘while’ loop – Repeats a statement or group of statements while an expression evaluates to true. There is a condition to get into the loop, so if the condition evaluates to true at the beginning, then the statements inside the loop will never execute.
- 'until' loop - Like a 'while' loop, with a condition to enter it, except the statements within execute while an expression is false.
- ‘do while’ loop – This is similar to the ‘while’ loop except the condition is at the end, so the statements within the loop will always execute once.
‘for’ loop
The ‘for’ loop takes the following form.
for (initialiser; condition; iterator) { # Statement(s) to execute. }
The initialiser is usually a local variable set up as a loop counter. The condition is an expression that is checked before each iteration of the loop, which must evaluate to true for the statements within the loop to be executed. Finally, the iterator is an expression that is executed at the end of each loop, which usually increments the loop counter.
The following example outputs to the screen the value of the loop counter for each iteration of the loop.
for (my $i = 0; $i < 5; $i++) { print "$i\n"; }
Here the initialiser is set to zero and the condition is checking for a value of less than five. With the iterator adding one to the loop counter each time, it means that the statement within the loop will be executed five times.
0 1 2 3 4
‘foreach’ loop
A 'foreach' loop works with a range or collection of values.
foreach (range/collection) { # Statement(s) to execute. }
In the case of the below example a range between zero and four is used to produce the same output as the above 'for' loop.
foreach my $i (0..4) { print "$i\n"; }
‘while’ loop
As mentioned above, the ‘while’ loop has a condition, which must evaluate to true before the statements within it can be executed.
while (condition) { # Statement(s) to execute. }
In the below example, a variable is declared and initialised to zero, then the ‘while’ loop checks to see that the variable is less than five before outputting it to the screen and incrementing the variable by one.
my $i = 0; while ($i < 5) { print "$i\n"; $i++; }
The output will be the same as in the examples above. Note that if the variable ‘$i’ had been declared and initialised to five or above, then the statements within the loop would not be executed at all.
'until' loop
The format of an 'until' loop is similar to a 'while' loop, with the 'while' reserved word replaced with 'until'. The condition here must evaluate to false for the statements within to execute.
until (condition) { # Statement(s) to execute. }
The following example declares and initialises a variable to zero and the loop condition checks for a value that is greater than or equal to five. The code in this loop executes whilst this condition evaluates to false. As with the 'while' loop, the variable is incremented by one with each iteration. The output is again the same as previous examples.
my $i = 0; until ($i >= 5) { print "$i\n"; $i++; }
‘do while’
The final loop, a ‘do while’ loop, differs from those previously discussed as it has a condition to exit, rather than enter, the loop. This means that the statements within the loop will always execute once, regardless of the condition.
do { # Statement(s) to execute. } while (condition)
The below example declares and initialises a variable to zero, then within the loop displays its value and increments it by one. The condition at the end checks for a value that is less than five.
my $i = 0; do { print "$i\n"; $i++; } while ($i < 5);
The resulting output is again the same as in previous examples.