← Previous Next →


Perl Loops

In general a program flow looks something like this:

Here, the control flows sequentially statement after statement until the last line executes. This will not work if want to repeat certain steps until a particular condition is reached. For that purpose we use loops. The control flows in a loop something like this:

This is used when we want to execute a block of code repeatedly. A loop statement allows us to execute a statement or group of statements multiple times.

Perl provides the following looping statements:

  • while loop - Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. Syntax is: 
    while(condition) {
       statement(s);
    }
  • until loop - Repeats a statement or group of statements until a given condition becomes true. It tests the condition before executing the loop body. Syntax is: 
    until(condition)
    {
       statement(s);
    }
  • for loop - Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. Syntax is: 
    for ( init; condition; increment ){
       statement(s);
    }
  • 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. Syntax is: 
    foreach var (list) {
    ...
    }
  • do...while loop - Like a while statement, except that it tests the condition at the end of the loop body. Syntax is: 
    do {
       statement(s);
    }while( condition );
    
  • nested loop - We can use one or more loop inside any another while, for or do..while loop. Syntax is: 
    for ( init; condition; increment ){
       for ( init; condition; increment ){
          statement(s);
       }
       statement(s);
    }

Loop Control Statements

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.
The control statements available in Perl are:

  • next statement - Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. Syntax 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.

  • last statement - Terminates the loop statement and transfers execution to the statement immediately following the loop. Syntax is: 
    last [LABEL];
  • continue statement - A continue BLOCK, it is always executed just before the conditional is about to be evaluated again. Syntax is: 
    continue{
       statement(s);
    }
  • redo statement - The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed. Syntax is: 
    redo [LABEL]
  • goto statement - Perl supports a goto command with three forms: goto label, goto expr, and goto &name. Syntax is: 
    goto LABEL

 

 

← Previous Next →