Professional Documents
Culture Documents
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.
Sr.No. Loop Type & Description
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.
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.
3 for loop
Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.
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.
5 do...while loop
Like a while statement, except that it tests the condition at the end of the loop body
6 nested loops
You can use one or more loop inside any another while, for or do..while loop.
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.
Sr.No. Control Statement & Description
1 next statement
Causes the loop to skip the remainder of its body and immediately retest its condition prior
to reiterating.
2 last statement
Terminates the loop statement and transfers execution to the statement immediately
following the loop.
3 continue statement
4 redo statement
The redo command restarts the loop block without evaluating the conditional again. The
continue block, if any, is not executed.
5 goto statement
Perl supports a goto command with three forms: goto label, goto expr, and goto &name.
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.
Perl while Loop
A while loop statement in Perl programming language repeatedly executes a target statement as long
as a given condition is true.
Syntax
The 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.
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.
Flow Diagram
Here the key point of the while loop is that the loop might not ever run. When the condition is tested
and the result is false, the loop body will be skipped and the first statement after the while loop will be
executed.
Example
Live Demo
#!/usr/local/bin/perl
$a = 10;
Here we are using the comparison operator < to compare value of variable $a against 20. So while
value of $a is less than 20, while loop continues executing a block of code next to it and as soon as
the value of $a becomes equal to 20, it comes out. When executed, above code 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
Perl until Loop
An until loop statement in Perl programming language repeatedly executes a target statement as long
as a given condition is false.
Syntax
The 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.
Flow Diagram
Here key point of the until loop is that the loop might not ever run. When the condition is tested and the
result is true, the loop body will be skipped and the first statement after the until loop will be executed.
Example
Live Demo
#!/usr/local/bin/perl
$a = 5;
Here we are using the comparison operator > to compare value of variable $a against 10. So until the
value of $a is less than 10, until loop continues executing a block of code next to it and as soon as the
value of $a becomes greater than 10, it comes out. When executed, above code produces the
following result −
Value of a: 5
Value of a: 6
Value of a: 7
Value of a: 8
Value of a: 9
Value of a: 10
Perl do...while Loop
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop
checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at
least one time.
Syntax
The 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.
Flow Diagram
Example
Live Demo
#!/usr/local/bin/perl
$a = 10;
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
Perl for Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
Syntax
The syntax of a for loop in Perl programming language is −
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.
Flow Diagram
Example
Live Demo
#!/usr/local/bin/perl
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
Perl foreach Loop
The foreach loop iterates over a list value and sets the control variable (var) to be each element of the
list in turn −
Syntax
The syntax of a foreach loop in Perl programming language is −
Flow Diagram
Example
Live Demo
#!/usr/local/bin/perl
value of a: 2
value of a: 20
value of a: 30
value of a: 40
value of a: 50
Perl next Statement
The Perl next statement starts the next iteration of the loop. You can provide a LABEL with next
statement where LABEL is the label for a loop. A next statement can be used inside a nested loop
where it will be applicable to the nearest loop if a LABEL is not specified.
If there is a continue block on the loop, it is always executed just before the condition is about to be
evaluated. You will see the continue statement in separate chapter.
Syntax
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.
Flow Diagram
Example
Live Demo
#!/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;
}
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 −
Live Demo
#!/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;
}
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
Perl last Statement
When a last statement is encountered inside a loop, the loop is immediately terminated and the
program control resumes at the next statement following the loop. You can provide a LABEL with last
statement where LABEL is the label for a loop. A last statement can be used inside a nested loop
where it will be applicable to the nearest loop if a LABEL is not specified.
If there is any continue block on the loop, then it is not executed. You will see the continue statement
in a separate chapter.
Syntax
The syntax of a last statement in Perl is −
last [LABEL];
Flow Diagram
Example 1
Live Demo
#!/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;
}
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
Example 2
Let's take one example where we are going to use a LABEL along with next statement −
Live Demo
#!/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;
}
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