Perl do...while Loop

Perl 循环Perl Loops

Unlikeforandwhileloops, which test the loop condition at the top of the loop. In the Perl language,do...whilethe loop checks its condition at the bottom of the loop.

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

Syntax

The syntax format is as follows:

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

Note that the conditional expression appears at the bottom of the loop, so the statement(s) in the loop will be executed at least once before the condition is tested.

If the condition is true, the control flow jumps back to the do above, and re-executes the statement(s) in the loop. This process repeats until the given condition becomes false.

Flowchart

Perl 中的 do...while 循环

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 );

After executing the above program, the output is:

a 的值为: 10
a 的值为: 11
a 的值为: 12
a 的值为: 13
a 的值为: 14

Perl 循环Perl Loops

Other Extensions