Easy Tutorial
❮ Perl Process Management Perl Foreach Loop ❯

Perl do...while Loop

Perl Loops

Unlike the for and while loops, which test the loop condition at the beginning, the do...while loop in Perl checks its condition at the end of the loop.

The do...while loop is similar to the while loop, but it ensures that the loop is executed at least once.

Syntax

The syntax is as follows:

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

Note that the condition expression appears at the end of the loop, so the statement(s) within the loop are executed at least once before the condition is tested.

If the condition is true, the control flow jumps back to the do statement, and the statement(s) within the loop are executed again. This process repeats until the given condition becomes false.

Flowchart

Example

#!/usr/bin/perl

$a = 10;

# Execute do...while loop
do{
   printf "The value of a is: $a\n";
   $a = $a + 1;
}while( $a < 15 );

Executing the above program will output:

The value of a is: 10
The value of a is: 11
The value of a is: 12
The value of a is: 13
The value of a is: 14

Perl Loops

❮ Perl Process Management Perl Foreach Loop ❯