无涯教程-Perl - do...while 语句函数

forwhile 循环不同,它在循环的顶部测试循环条件,而 do ... while 循环在以下位置检查其条件:循环的底部。

do ... while 循环与while循环相似,除了保证do ... while循环至少执行一次。

do...while - 语法

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

应当注意,条件表达式出现在循环的末尾,因此循环中的语句在测试条件之前执行一次。

do...while - 流程图

do...while - 示例

#!/usr/local/bin/perl
 
$a=10;

# do...while loop execution
do{
   printf "Value of a: $a\n";
   $a=$a + 1;
}while( $a < 20 );

执行以上代码后,将产生以下输出-

Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
Value of a: 18
Value of a: 19

Perl 中的 do...while 语句函数 - 无涯教程网无涯教程网提供与 for 和 while 循环不同,它在循环的顶部测试循环条件,而 do ... while 循环在以下...https://www.learnfk.com/perl/perl-do-while-loop.html

你可能感兴趣的:(无涯教程,perl)