Latest web development tutorials

Perl while loop

Perl cycle Perl cycle

while the statement at a given condition is true, statement or group of statements repeatedly performed. The test conditions will be executed before the body of the loop.

grammar

The syntax is as follows:

while(condition)
{
   statement(s);
}

Here, statement (s) may be a single statement, it can also be a block composed of a few statements. condition can be any expression, when an arbitrary non-zero values ​​are true. Execution of the loop when the condition is true.

When the condition is false, program flow will continue with the next statement followed the loop.

flow chart

Perl while loop

Chart, key pointswhileloop is a loop may not execute. When the condition is false, the loop body will skip directly to the next statement immediately while loop.

Examples

#!/usr/bin/perl

$a = 10;

# 执行 while 循环
while( $a < 20 ){
   printf "a 的值为 : $a\n";
   $a = $a + 1;
}

When the loop body is executed when the program is in the variable $ a is less than 20, in the variable $ a is greater than or equal to 20 to exit the loop.

The above program, the output is:

a 的值为 : 10
a 的值为 : 11
a 的值为 : 12
a 的值为 : 13
a 的值为 : 14
a 的值为 : 15
a 的值为 : 16
a 的值为 : 17
a 的值为 : 18
a 的值为 : 19

Perl cycle Perl cycle