Perl do ... while loop
Unlike thefor and whileloops, they are testing the loop condition of the loop. InPerl, do ... while loop is to check its condition at the end of the loop.
do ... while loop is similar to the while loop, but do ... while loop will ensure the implementation of at least a cycle.
grammar
The syntax is as follows:
do { statement(s); }while( condition );
Please note that the conditional expressions appear at the end of the loop, so the loop statement (s) will be executed at least once before the condition is tested.
If the condition is true, the flow of control jumps back above do, and then re-execute the loop statement (s). This process is repeated until the given condition becomes false.
flow chart
Examples
#!/usr/bin/perl $a = 10; # 执行 do...while 循环 do{ printf "a 的值为: $a\n"; $a = $a + 1; }while( $a < 15 );
The above program, the output is:
a 的值为: 10 a 的值为: 11 a 的值为: 12 a 的值为: 13 a 的值为: 14