Perl - Loop
There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages −

Perl programming language provides the following types of loop to handle the looping requirements.
#1. while loop
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
SyntaxThe syntax of a while loop in Perl programming language is −
while(condition) { statement(s); }
Here statement(s) may be a single statement or a block of statements. The condition may be any expression. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop.
Example#!/usr/local/bin/perl $a = 10; # while loop execution while( $a < 20 ) { printf "Value of a: $a\n"; $a = $a + 1; }
#2. until loop
Repeats a statement or group of statements until a given condition becomes true. It tests the condition before executing the loop body.
SyntaxThe syntax of an until loop in Perl programming language is −
until(condition) { statement(s); }
Here statement(s) may be a single statement or a block of statements. The condition may be any expression. The loop iterates until the condition becomes true. When the condition becomes true, the program control passes to the line immediately following the loop.
The number 0, the strings '0' and "" , the empty list () , and undef are all false in a boolean context and all other values are true. Negation of a true value by ! or not returns a special false value.
Example#!/usr/local/bin/perl $a = 5; # until loop execution until( $a > 10 ) { printf "Value of a: $a\n"; $a = $a + 1; }
#3. for loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
SyntaxThe syntax of a for loop in Perl programming language is −
for ( init; condition; increment ) { statement(s); }
Here is the flow of control in a for loop −
- The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
- Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
- After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
- The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
# for loop execution for( $a = 10; $a < 20; $a = $a + 1 ) { print "value of a: $a\n"; }
When the above code is executed, it produces the following result −
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
#4. foreach loop
The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn.
SyntaxThe syntax of a foreach loop in Perl programming language is −
foreach var (list) { ... }Example
#!/usr/local/bin/perl @list = (2, 20, 30, 40, 50); # foreach loop execution foreach $a (@list) { print "value of a: $a\n"; }
When the above code is executed, it produces the following result −
value of a: 2 value of a: 20 value of a: 30 value of a: 40 value of a: 50
#5. do...while loop
Like a while statement, except that it tests the condition at the end of the loop body
SyntaxThe syntax of a do...while loop in Perl is −
do { statement(s); }while( condition );
It should be noted that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false.
The number 0, the strings '0' and "" , the empty list () , and undef are all false in a boolean context and all other values are true. Negation of a true value by ! or not returns a special false value.
Example#!/usr/local/bin/perl $a = 10; # do...while loop execution do{ printf "Value of a: $a\n"; $a = $a + 1; }while( $a < 20 );
When the above code is executed, it produces the following result −
Value of a: 10 Value of a: 11 Value of a: 12 Value of a: 13 Value of a: 14 Value of a: 15 Value of a: 16 Value of a: 17 Value of a: 18 Value of a: 1
#6. nested loops
You can use one or more loop inside any another while, for or do..while loop.
SyntaxThe syntax for a nested for loop statement in Perl is as follows −
for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
The syntax for a nested while loop statement in Perl is as follows −
while(condition) { while(condition) { statement(s); } statement(s); }
The syntax for a nested do...while loop statement in Perl is as follows −
do{ statement(s); do{ statement(s); }while( condition ); }while( condition );
The syntax for a nested until loop statement in Perl is as follows −
until(condition) { until(condition) { statement(s); } statement(s); }
The syntax for a nested foreach loop statement in Perl is as follows −
foreach $a (@listA) { foreach $b (@listB) { statement(s); } statement(s); }
The following program uses a nested while loop to show the usage −
#/usr/local/bin/perl $a = 0; $b = 0; # outer while loop while($a < 3) { $b = 0; # inner while loop while( $b < 3 ) { print "value of a = $a, b = $b\n"; $b = $b + 1; } $a = $a + 1; print "Value of a = $a\n\n"; }
This would produce the following result −
value of a = 0, b = 0 value of a = 0, b = 1 value of a = 0, b = 2 Value of a = 1 value of a = 1, b = 0 value of a = 1, b = 1 value of a = 1, b = 2 Value of a = 2 value of a = 2, b = 0 value of a = 2, b = 1 value of a = 2, b = 2 Value of a = 3
Loop control statements change the execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Perl supports the following control statements. Click the following links to check their detail.
#1. next statement
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
SyntaxThe syntax of a next statement in Perl is −
next [ LABEL ];
A LABEL inside the square braces indicates that LABEL is optional and if a LABEL is not specified, then next statement will jump the control to the next iteration of the nearest loop.
Example#!/usr/local/bin/perl $a = 10; while( $a < 20 ) { if( $a == 15) { # skip the iteration. $a = $a + 1; next; } print "value of a: $a\n"; $a = $a + 1; }
When the above code is executed, it produces the following result −
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19
Let's take one example where we are going to use a LABEL along with next statement −
#!/usr/local/bin/perl $a = 0; OUTER: while( $a < 4 ) { $b = 0; print "value of a: $a\n"; INNER:while ( $b < 4) { if( $a == 2) { $a = $a + 1; # jump to outer loop next OUTER; } $b = $b + 1; print "Value of b : $b\n"; } print "\n"; $a = $a + 1; }
When the above code is executed, it produces the following result −
value of a : 0 Value of b : 1 Value of b : 2 Value of b : 3 Value of b : 4 value of a : 1 Value of b : 1 Value of b : 2 Value of b : 3 Value of b : 4 value of a : 2 value of a : 3 Value of b : 1 Value of b : 2 Value of b : 3 Value of b : 4
#2. last statement
Terminates the loop statement and transfers execution to the statement immediately following the loop.
SyntaxThe syntax of a last statement in Perl is −
last [LABEL];Example 1
#!/usr/local/bin/perl $a = 10; while( $a < 20 ) { if( $a == 15) { # terminate the loop. $a = $a + 1; last; } print "value of a: $a\n"; $a = $a + 1; }
When the above code is executed, it produces the following result −
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14
Let's take one example where we are going to use a LABEL along with next statement −
#!/usr/local/bin/perl $a = 0; OUTER: while( $a < 4 ) { $b = 0; print "value of a: $a\n"; INNER:while ( $b < 4) { if( $a == 2) { # terminate outer loop last OUTER; } $b = $b + 1; print "Value of b : $b\n"; } print "\n"; $a = $a + 1; }
When the above code is executed, it produces the following result −
value of a : 0 Value of b : 1 Value of b : 2 Value of b : 3 Value of b : 4 value of a : 1 Value of b : 1 Value of b : 2 Value of b : 3 Value of b : 4 value of a : 2
#3. continue statement
A continue BLOCK, it is always executed just before the conditional is about to be evaluated again.
SyntaxThe syntax for a continue statement with while loop is as follows −
while(condition) { statement(s); } continue { statement(s); }
The syntax for a continue statement with foreach loop is as follows −
foreach $a (@listA) { statement(s); } continue { statement(s); }
The syntax for a continue statement with a BLOCK of code is as follows −
continue { statement(s); }
The following program simulates a for loop using a while loop −
#/usr/local/bin/perl $a = 0; while($a < 3) { print "Value of a = $a\n"; } continue { $a = $a + 1; }
This would produce the following result −
Value of a = 0 Value of a = 1 Value of a = 2
The following program shows the usage of continue statement with foreach loop −
#/usr/local/bin/perl @list = (1, 2, 3, 4, 5); foreach $a (@list) { print "Value of a = $a\n"; } continue { last if $a == 4; }
This would produce the following result −
Value of a = 1 Value of a = 2 Value of a = 3 Value of a = 4
#4. redo statement
The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed.
SyntaxThe syntax for a redo statement is as follows −
redo [LABEL]Example
The following program shows the usage of redo statement −
#/usr/local/bin/perl $a = 0; while($a < 10) { if( $a == 5 ) { $a = $a + 1; redo; } print "Value of a = $a\n"; } continue { $a = $a + 1; }
This would produce the following result −
Value of a = 0 Value of a = 1 Value of a = 2 Value of a = 3 Value of a = 4 Value of a = 6 Value of a = 7 Value of a = 8 Value of a = 9
#5. goto statement
Perl supports a goto command with three forms: goto label, goto expr, and goto &name.
SyntaxThe syntax for a goto statements is as follows −
goto LABEL or goto EXPR or goto &NAMEExample
The following program shows the most frequently used form of goto statement −
#/usr/local/bin/perl $a = 10; LOOP:do { if( $a == 15) { # skip the iteration. $a = $a + 1; # use goto LABEL form goto LOOP; } print "Value of a = $a\n"; $a = $a + 1; } while( $a < 20 );
When the above code is executed, it produces the following result −
Value of a = 10 Value of a = 11 Value of a = 12 Value of a = 13 Value of a = 14 Value of a = 16 Value of a = 17 Value of a = 18 Value of a = 19
Following example shows the usage of goto EXPR form. Here we are using two strings and then concatenating them using string concatenation operator (.). Finally, its forming a label and goto is being used to jump to the label −
#/usr/local/bin/perl $a = 10; $str1 = "LO"; $str2 = "OP"; LOOP:do { if( $a == 15) { # skip the iteration. $a = $a + 1; # use goto EXPR form goto $str1.$str2; } print "Value of a = $a\n"; $a = $a + 1; } while( $a < 20 );
When the above code is executed, it produces the following result −
Value of a = 10 Value of a = 11 Value of a = 12 Value of a = 13 Value of a = 14 Value of a = 16 Value of a = 17 Value of a = 18 Value of a = 19
A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.
#!/usr/local/bin/perl for( ; ; ) { printf "This loop will run forever.\n"; }
You can terminate the above infinite loop by pressing the Ctrl + C keys.
When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but as a programmer more commonly use the for (;;) construct to signify an infinite loop.
Comments
Post a Comment