★ wanayoo — archive 1999 http://uk.php.net/manual/ro/control-structures.do.while.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<whilefor>
view the version of this page
Last updated: Tue, 25 Nov 2003

do..while

do..while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do..while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it's may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it evaluates to FALSE right from the beginning, the loop execution would end immediately).

There is just one syntax for do..while loops:

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

The above loop would run one time exactly, since after the first iteration, when truth expression is checked, it evaluates to FALSE ($i is not bigger than 0) and the loop execution ends.

Advanced C users may be familiar with a different usage of the do..while loop, to allow stopping execution in the middle of code blocks, by encapsulating them with do..while(0), and using the break statement. The following code fragment demonstrates this:

<?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);
?>

Don't worry if you don't understand this right away or at all. You can code scripts and even powerful scripts without using this 'feature'.



add a note add a note User Contributed Notes
do..while
nightkin at msn dot com
24-Jun-2003 04: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
14-Jun-2003 06: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 01: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
05-Jan-2002 05: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.
reaverkin at yahoo dot com
22-Nov-2000 06:55
The point of the example is to illustrate a trick sometimes used to cleanly break out of a block of code. Consider the following. 

if (a == OK) {
  .. code ..
  if (b == OK) {
   .. code ..
  }
}

or alternatively something like:

if (a != OK)
  goto END;
.. code ..
if (b != OK)
  goto END;
.. code ..
END:

Both can get messy, and the latter uses goto statements which is frowned upon.
Instead we can exploit the do..while construct for its handy break mechanism:

do {
  if (a != OK)
   break;
  .. code ..
  if (b != OK)
   break;
  .. code ..
while (0);

<whilefor>
 Last updated: Tue, 25 Nov 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: Kewlio.net Limited
Last updated: Mon Dec 1 18:12:31 2003 GMT