Loops in PERL

                                                                                                                         

 

Loops in PERL

  • Loops allow you to run a block of code as many times as you want, as long as some condition evaluates to true.
  • Loops always have some condition attached to them.
Loops – Types Description
While Repeats a statement or group of statements while a given condition is true. It
tests the condition before executing the loop body.
untill Repeats a statement or group of statements until a given condition becomes
true. It tests the condition before executing the loop body.
For Execute a sequence of statements multiple times and abbreviates the code
that manages the loop variable.
foreach The foreach loop iterates over a normal list value and sets the variable VAR
to be each element of the list in turn.
do…while Like a while statement, except that it tests the condition at the
end of the loop body
nested loops You can use one or more loop inside any another while, for or
do..while loop.

 

 

While loop:

  • Syntax:

while(condition) {

statement(s);

}

  • Example:

#!/usr/bin/perl -w

$a = 10;

while( $a < 15 ) {

         printf “Value of a: $a\n”;

         $a = $a + 1;

}

Result:

     Value of a: 10

     Value of a: 11

     Value of a: 12

     Value of a: 13

     Value of a: 14

Until loop:

  • An until loop statement in Perl programming language repeatedly executes a target statement as long as a given condition is false.
  • Syntax:

until(condition)  {

                      statement(s);

}

  • Example:

#!/usr/bin/perl

$a = 5;

until( $a > 10 ) {

       printf “Value of a: $a\n”;

        a = $a + 1;

}

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

 

For loop:

  • Syntax:

for ( init; condition; increment ) {

statement(s);

}

  • Example:

for ( $i = 0; $i <= 10; $i=$i+2 ) {

print “$i\n”;

}

Result:

0

2

4

6

8

10

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:

foreach var (list) { … }

Example:

#!/usr/bin/perl

        @list = (2, 20, 30, 40, 50);

        foreach $a (@list) {

                print “value of a: $a\n”;

         }

Result:

value of a: 2

value of a: 20

value of a: 30

value of a: 40

value of a: 50

 

do while loop:

Syntax:

do {

     Statements; }

while (condition );

Example:

#!/usr/local/bin/perl

$i = 5;

do {

          print “value ofi: $i\n”;

        $i++;

}

while ($i < 7 && $i != 5);

Result:

value of i: 5

value of i: 6

 

last statement

The last statement can be used to terminate a loop, regardless of the condition’s value.

The last statement is similar to break in other programming languages.

Example:

while ($x < 10) {

     if ($x == 3) {last;}

     statements…

}

  • In this case, when $x is exactly 3, the last statement terminates the loop and execution continues past the closing curly brace.

next statement

  • The next statement causes execution of a loop to restart.
  • It is similar to the continue statement in some languages.
  • Any statements below the next statement are not executed.

Example:

while ($x < 10) {

       if ($x == 3) {next;}

        statements…

}

  • Once the next is encountered when $x is 3, the loop is restarted again, and the statements below the if are not executed on this pass.

 

 

                                                                                                                         

 

One thought on “Loops in PERL”

Leave a Reply

Your email address will not be published. Required fields are marked *