超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<whilefor>
Last updated: Fri, 22 Jun 2007

do..while

do..whilewhile 循环非常相似,区别在于表达式的值是在每次循环结束时检查而不是开始时。和正规的 while 循环主要的区别是 do..while 的循环语句保证会执行一次(表达式的真值在每次循环结束后检查),然而在正规的 while 循环中就不一定了(表达式真值在循环开始时检查,如果一开始就为 FALSE 则整个循环立即终止)。

do..while 循环只有一种语法:

<?php
$i
= 0;
do {
   print
$i;
} while (
$i > 0);
?>

以上循环将正好运行一次,因为经过第一次循环后,当检查表达式的真值时,其值为 FALSE($i 不大于 0)而导致循环终止。

资深的 C 语言用户可能熟悉另一种不同的 do..while 循环用法,把语句放在 do..while(0) 之中,在循环内部用 break 语句来结束执行循环。以下代码片段示范了此方法:

<?php
do {
   if (
$i < 5) {
       print
"i is not big enough";
       break;
   }
  
$i *= $factor;
   if (
$i < $minimum_limit) {
       break;
   }
   print
"i is ok";

  
/* process i */

} while(0);
?>

如果你还不能立刻理解也不用担心。即使不用此“特性”你也照样可以写出强大的代码来。




add a note add a note User Contributed Notes
do..while
nightkin at msn dot com
24-Jun-2003 11:15
You can use a "break n" statement to break within n cicles, so, if you have:

While (true) {
 While (true) {
   ... code ...
       break 2; // breaks out of both cycles
   ... code ...
       break; // just breaks out of the inner cycle
   ... code ...
 }
 ... code ...
     break;
 ... code ...
}

That way you have no reason to use goto's, but if you still want to use them, be my guest...
braintrino
15-Jun-2003 01:27
Oh, the example in the last post was not a good example.  I was wrong -- it will cycle infinitely.

I was trying to translate that scenario from the "event-manager code," which is similar, but have a distinct difference, i.e., the event-manager code has a "wait-state" so it waits rather than continually executing through the loop.  When it is in the wait state (for an event to occur), it is not wasting or stealing any CPU time.  That is why it won't tie up the system in the event-manager code:

do
{
   WaitForEvent();   //the events can be mouse-event, keyboard-event, disk-event, network-event, clock-event, etc.
   ProcessEvent();    //send the event to the appropriate event-handler to do something with it
} while (true);

So, in this case, you would not want to exit the event-listening code.  Otherwise, the computer will stop responding to any external event entirely, i.e., it is in a coma, and the system hangs.  So long as the system is up, it will need to listen to some sort of events to execute accordingly.
malgat
21-Feb-2003 09:31
It's all very well to use the do..while construct as a method of avoiding the goto statement, but please be aware that this can become messy if you have embedded while, for or switch statements, which can also be break'ed.

In these cases, the code might very well look cleaner with the dreaded goto statement.  As long as the goto direction is "downward", its use should only invoke a slight grimace.
sebbeNOSPAM at popmail dot com
06-Jan-2002 01:09
A do..while loop ALWAYS gets executed at least ONE time, since the truth statement is in the end of the loop. A while loop differs from this, since the truth statement is in the beginning it may never get executed if it's false.

<whilefor>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22