Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl do...while Loop

Perl do...while Loop

Perl Loops Perl Loops

Unlike for and while loops, which test the loop condition at the head of the loop. In the Perl language,do...while The do...while loop checks its condition at the bottom of the loop.

do...while The do...while loop is similar to the while loop, but it guarantees the loop body executes at least once.

Syntax

The syntax format is as follows:



do

{

   statement(s);

}while( condition );

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

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

Flowchart

The do...while Loop in Perl

Example

#!/usr/bin/perl

$a

=

10

;

Execute a do...while loop

do

{

printf

"

The value of a is:

$a

\n

"

;

$a

=

$a

1

;

}

while

(

$a

<

15

)

;

Running the above program, the output is:



a 的值为: 10

a 的值为: 11

a 的值为: 12

a 的值为: 13

a 的值为: 14

Perl Loops Perl Loops