Perl Loop

The Loop Statements

Different loop statements in perl…

  • The for Loop
  • The foreach Loop
  • The while Loop
  • The do-while Loop
  • The until Loop

The process of executing a code block repetitively is known as

iteration. To perform iteration in applications, use the loop

statements, such as for and while.

The loop statements check a condition and repetitively execute the

enclosed statements until the condition is true. The loop

terminates only when the condition becomes invalid.

The for Loop

This loop is used to execute a given set of statements for a fixed

number of times. The syntax of the for loop is:


for(initialization;testing;updation)

{

	block of statement(s);

}

In these statements:

initialization: Is the code to declare and initialize the

loop counter. Loop counter is a variable to keep a check on the

number of iterations. Initialization happens only once before the

beginning of the loop.

testing: Is the code, which specifies the condition to

control the number of iterations. The loop executes until the

result of testing is true. When the condition becomes false, the

control passes to the statement following the loop.

updation: Is the code to modify the loop counter after each

iteration. It can increment or decrement the loop counter,

according to the program requirements. Updation occurs at the end

of the loop.

block of statement(s): Is the code to be executed

iteratively. This code must be enclosed within the curly braces.

Note The three expressions for initialization, condition, and

updation are optional. If you leave the condition expression empty,

the for loop will be an infinite loop.


#! /usr/bin/perl

print "Enter a digit to create its table: ";

$a = <>;

chomp($a);

for($b=1;$b<=10;$b++)

{

	print $a.' x '.$b.' = '. $a*$b."n";

}

  • $b=1 is the initialization statement, which initializes the

    loop counter $b to 1.

  • $b<=10 is the testing statement, which checks whether the value stored in $b is less than or equal to 10.
  • $b++ is the updation statement, which increments the value of

    $b by 1.

    The Nested for Loop

    A for loop contained inside another for loop is called a nested for

    loop. This is used when the data is to be stored and printed in a

    tabular format having multiple rows and columns.

    
    #! /usr/bin/perl
    
    for($a=0;$a<=9;$a++){
    
            for($b=0;$b<=$a;$b++){
    
                    print "*";
    
            }
    
            print "n";
    
    } 
    
    

    In this program:

    • The outer loop works until the value of $a is less than or

      equal to 9.

    • The inner loop works until the value of $b is less than or

      equal to the value of $a.

    • The newline character n is used to enter a newline after every

      row.

    Note The nested for loops are used with multidimensional arrays.

    For more information on arrays.

    The foreach Loop

    This loop operates on arrays. An array stores multiple related

    values in a row that can be accessed easily using the foreach loop.

    The syntax of the foreach loop is:

    
    foreach $var_name (@array_name)
    
    {
    
    	block of statement(s);
    
    }
    
    

    In this syntax:

    • @array_name is the array whose elements are accessed using the

      foreach loop.

    • $var_name is the scalar variable that stores the value of

      element of @array_name for each iteration.

    • This loop is repeated for all the elements in the array. The

      code used for the foreach loop is:

    
    #! /usr/bin/perl
    
    @names = ("George", "Jack", "Davis");
    
    foreach $word(@names)
    
    {
    
    print "$wordn";
    
    }
    
    

    This example, when executed, prints values of all the elements in

    the @names array one-by-one using the foreach loop. The output of

    the example is shown in Figure 4-5:

    The while Loop

    There may be situations when you do not know the number of times a

    loop is to be executed. For example, an application accepts and

    stores the scores of students in a class, and you do not know the

    number of students in a class. In this example, you can use the

    while loop.


    The while loop executes as long as the condition specified is true.

    The condition can be any valid relational expression, which returns

    true or false.

    This loop is also known as Pre-Check or Pre-Tested Looping

    Construct because the condition is checked before executing the

    statement(s) in the block.

    The syntax of the while loop is:

    
    while (condition)
    
    {
    
    	block of statement(s);
    
    }
    
    

    In these statements, block of statement(s) is executed only if the

    condition is true.

    Note The code block must be enclosed within curly braces.

    
    #! /usr/bin/perl
    
    $a = 1;
    
    while ($a <= 10)
    
    {
    
    	print "$an";
    
    	$a++;
    
    }
    
    

    In this example:

    • $a, the loop counter is initialized to 1.
    • The condition checks whether $a is less than or equal to 10.
    • The value of $a is incremented by 1 in each iteration using the

      post increment operator (++).

    • The loop prints the numbers from 1 to 10 until the value of $a is less than or equal to 10. When $a is incremented to 11, the condition results in false, and the loop terminates.

    The do-while Loop

    The do-while loop is used when you want to execute a code block at

    least once unconditionally, and then iteratively on the basis of a

    condition.

    In this loop, condition is tested at the end of the loop. Because

    of this, this loop is also known as Post-Check or Post Tested

    Looping Construct.

    The syntax of the do-while loop is:

    
    do 
    
    {
    
    	block of statement(s);
    
    }
    
    while (condition);
    
    

    
    #! /usr/bin/perl
    
    $a = 2;
    
    do
    
    {
    
    	print "$an";
    
    	$a+=2;
    
    } while ($a<=20);
    
    

    In this example:

    • $a, the loop counter is initialized to 2.
    • The loop prints the value of $a and also increments it by 2.
    • The condition associated with while tests whether the value of

      $a is less than or equal to 20.

    • The loop prints even numbers until the value of $a is less than

      or equal to 20. When $a is equal to 22, the loop terminates.

    The until Loop

    In case of the while loop, the code that follows condition is

    executed only if the condition is true. In the case of the until

    loop, code associated with the condition is executed only if the

    condition is false. The syntax of the until loop is:

    
    until(condition)
    
    {
    
    	block of statement(s);
    
    }
    
    

    In this syntax, the block of statement(s) is executed only when the

    condition returns false.

    
    #! /usr/bin/perl
    
    $a = 1;
    
    until(a == 11)
    
    {
    
    	print $a."n";
    
    $a++;
    
    }
    
    

    In this program:

    • $a, the loop counter is initialized to 1.
    • The condition checks whether $a is equal to 11.
    • The loop prints and increments the value of $a.
    • The condition returns false until $a is less than 11, and the

      code in the loop prints from 1 to 10. The moment $a is equal to 11,

      the condition is met and loop is terminated.

  • Previous Post

    Next Post

    Comments are closed.