Perl until Loop
The until
statement repeatedly executes a statement or a group of statements while the given condition is false.
until
can be translated as "unless".
Syntax
The syntax is as follows:
until(condition)
{
statement(s);
}
Here, statement(s)
can be a single statement or several statements forming a code block.
condition
can be any expression, and the loop executes when the condition is false.
When the condition is true, the program flow will continue to the statement immediately following the loop.
Flowchart
In the flowchart, the key point of the until
loop is that the loop may not execute even once. When the condition is true, it skips the loop body and proceeds to the statement immediately following the while
loop.
Example
#!/usr/bin/perl
$a = 5;
# Execute until loop
until( $a > 10 ){
printf "a 的值为 : $a\n";
$a = $a + 1;
}
The loop body in the program executes while the variable $a
is less than 10, and it exits the loop when $a
is greater than 10.
Executing the above program will output:
a 的值为 : 5
a 的值为 : 6
a 的值为 : 7
a 的值为 : 8
a 的值为 : 9
a 的值为 : 10