Php 简明教程
PHP - Do…While Loop
"do…while" 循环是 PHP 中提供的另一种循环构造。此类型的循环类似于 while 循环,只是测试条件在每次迭代的末尾检查,而不是在新迭代的开始检查。
The "do…while" loop is another looping construct available in PHP. This type of loop is similar to the while loop, except that the test condition is checked at the end of each iteration rather than at the beginning of a new iteration.
while 循环在进入循环之前验证真条件,而在 "do…while" 循环中,真条件在重新进入循环之前得到验证。因此,无论真条件如何,"do…while" 循环都保证至少有一次迭代。
The while loop verifies the truth condition before entering the loop, whereas in "do…while" loop, the truth condition is verified before re entering the loop. As a result, the "do…while" loop is guaranteed to have at least one iteration irrespective of the truth condition.
下图通过使用两个比较流程图表示形式,展示了 "while" 循环和 "do…while" 循环的区别。
The following figure shows the difference in "while" loop and "do…while" loop by using a comparative flowchart representation of the two.
构成“do…while”循环的 syntax 与 C 语言中对应的部分类似。
The syntax for constituting a "do…while" loop is similar to its counterpart in C language.
do {
statements;
}
while (expression);
Example
这里是一个“do…while”循环的简单示例,它打印从 1 到 5 的迭代编号。
Here is a simple example of "do…while" loop that prints iteration numbers 1 to 5.
<?php
$i=1;
do{
echo "Iteration No: $i \n";
$i++;
}
while ($i<=5);
?>
它将生成以下 output −
It will produce the following output −
Iteration No: 1
Iteration No: 2
Iteration No: 3
Iteration No: 4
Iteration No: 5
Example
以下代码使用 while 循环,也会生成相同的输出 −
The following code uses a while loop and also generates the same output −
<?php
$i=1;
while ($i<=5){
echo "<h3>Iteration No: $i</h3>";
$i++;
}
?>
因此,可以说“do…while”和“while”循环表现得类似。然而,当计数器变量(在这种情况下为 $i )的初始值被设置为任何大于 while 关键字前面的括号中的测试表达式中使用的一个,就会体现出差异。
Hence, it can be said that the "do…while" and "while" loops behave similarly. However, the difference will be evident when the initial value of the counter variable (in this case $i) is set to any value more than the one used in the test expression in the parenthesis in front of the while keyword.
Example
在以下代码中,使用了两个循环 – while 和 “ do…while ”。 while 循环的计数器变量是 $i ,而“do…while”循环的计数器变量是 $j 。两者都被初始化为 10(任何大于 5 的值)。
In the following code, both the loops – while and "do…while" – are used. The counter variable for the while loop is $i and for "do…while" loop, it is $j. Both are initialized to 10 (any value greater than 5).
<?php
echo "while Loop \n";
$i=10;
while ($i<=5){
echo "Iteration No: $i \n";
$i++;
}
echo "do-while Loop \n";
$j=10;
do{
echo "Iteration No: $j \n";
$j++;
}
while ($j<=5);
?>
它将生成以下 output −
It will produce the following output −
while Loop
do - while Loop
Iteration No: 10
结果表明 while 循环不会执行任何迭代,因为条件在一开始就是假的( $i 被初始化为 10,这大于测试条件 $i⇐5 )。另一方面,“do…while”循环确实会进行第一次迭代,即使计数器变量 $j 被初始化为大于测试条件的值也是如此。
The result shows that the while loop doesn’t perform any iterations as the condition is false at the beginning itself ($i is initialized to 10, which is greater than the test condition $i⇐5). On the other hand, the "do…while" loop does undergo the first iteration even though the counter variable $j is initialized to a value greater than the test condition.
因此,我们可以推断出“do…while”循环至少可以保证一次迭代,因为测试条件是在循环块的末尾验证的。 while 循环可能不会执行任何迭代,因为在进入循环块之前验证了测试条件。
We can thus infer that the "do…while" loop guarantees at least one iteration because the test condition is verified at the end of looping block. The while loop may not do any iteration as the test condition is verified before entering the looping block.
另一个语法差异是“do…while”中的 while 语句以分号结尾。对于 while 循环,圆括号后面是一个大括号循环块。
Another syntactical difference is that the while statement in "do…while" terminates with semicolon. In case of while loop, the parenthesis is followed by a curly bracketed looping block.
除此之外,没有其他差异。可以互换使用这两种类型的循环。
Apart from these, there are no differences. One can use both these types of loops interchangeably.
Decrementing a "do…while" Loop
要设计一个带递减计数的“do…while”,请将计数器变量初始化为一个较大的值,在循环内使用递减运算符 (--) 以在每次迭代时减少计数器的值,并将 while 括号中的测试条件设置为在计数器大于所需最后值时运行循环。
To design a "do…while" with decrementing count, initialize the counter variable to a higher value, use decrement operator (--) inside the loop to reduce the value of the counter on each iteration, and set the test condition in the while parenthesis to run the loop till the counter is greater than the desired last value.
Traverse a String in Reverse Order
在 PHP 中,一个字符串可以被视为一个带有字符的索引数组。我们可以通过运行一个递减的“do…while”循环从尾端到开头依次提取和显示一个个字符,如下所示 −
In PHP, a string can be treated as an indexed array of characters. We can extract and display one character at a time from end to beginning by running a decrementing "do…while" loop as follows −
<?php
$string = "TutorialsPoint";
$j = strlen($string);
do{
$j--;
echo "Character at index $j : $string[$j] \n";
}
while ($j>=1);
?>
它将生成以下 output −
It will produce the following output −
Character at index 13 : t
Character at index 12 : n
Character at index 11 : i
Character at index 10 : o
Character at index 9 : P
Character at index 8 : s
Character at index 7 : l
Character at index 6 : a
Character at index 5 : i
Character at index 4 : r
Character at index 3 : o
Character at index 2 : t
Character at index 1 : u
Character at index 0 : T
Nested "do…while" Loops
就像 for 循环或 while 循环一样,你也可以编写嵌套的“do…while”循环。在以下示例中,上面的“do…while”循环使用 $i 计数迭代,里面的“do…while”循环递增 $j ,并且每次打印 $i*j 的乘积,从而打印出从 1 到 10 的乘法表。
Like the for loop or while loop, you can also write nested "do…while" loops. In the following example, the upper "do…while" loop counts the iteration with $i, the inner "do…while" loop increments $j and each time prints the product of $i*j, thereby printing the tables from 1 to 10.
<?php
$i=1;
$j=1;
do{
print "\n";
do{
$k = sprintf("%4u",$i*$j);
print "$k";
$j++;
}
while ($j<=10);
$j=1;
$i++;
}
while ($i<=10);
?>
它将生成以下 output −
It will produce the following output −
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100