Page 1 of 1

why use do while?

Posted: Fri Jun 29, 2007 10:51 am
by Luke
I have never used the do while loop in my entire history with PHP or any other language. I was just looking through HTML Purifier to try and make it less "magical" in my head and get a feeling for just how in the world it does what it does, and I came across this:

Code: Select all

if (is_null($lexer)) { do {
            // auto-detection algorithm
            
            // once PHP DOM implements native line numbers, or we
            // hack out something using XSLT, remove this stipulation
            if ($config->get('Core', 'MaintainLineNumbers')) {
                $lexer = 'DirectLex';
                break;
            }
            
            if (version_compare(PHP_VERSION, "5", ">=") && // check for PHP5
                class_exists('DOMDocument')) { // check for DOM support
                $lexer = 'DOMLex';
            } else {
                $lexer = 'DirectLex';
            }
            
        } while(0); } // do..while so we can break
I can understand why he did this (because he put the comments in). Simply so he can break out of the do{} should he need to. But couldn't this have been accomplished just as easily by doing:

Code: Select all

if (is_null($lexer)) {
            // auto-detection algorithm
            
            // once PHP DOM implements native line numbers, or we
            // hack out something using XSLT, remove this stipulation
            if ($config->get('Core', 'MaintainLineNumbers')) {
                $lexer = 'DirectLex';
                break;
            }
            else {
                if (version_compare(PHP_VERSION, "5", ">=") && // check for PHP5
                    class_exists('DOMDocument')) { // check for DOM support
                    $lexer = 'DOMLex';
                } else {
                    $lexer = 'DirectLex';
                }
            }
            
        } // do..while so we can break
Is this simply a preference? Is there some advantage either way? What are some other uses for the do while syntax that made it worth putting in the language?

(Sorry to use your code for an example AC :oops: )

Posted: Fri Jun 29, 2007 10:58 am
by ReverendDexter
The difference between while, and do-while, is that you will always go into a do-while loop at least once.

It's useful if you want to have intentional almost infinite loops, i.e. if you write a program that listens on a socket, you want it to keep checking (listening) until you have a connection. There's other things they're useful for, but I didn't get enough sleep. I'll let the others beat it to hamburger :)

Posted: Fri Jun 29, 2007 11:09 am
by Luke
I understand WHAT they do, I just have never run into any instance where that would be useful.

Posted: Fri Jun 29, 2007 11:23 am
by califdon
It's a matter of choice, basically, like the difference between a switch statement and a series of if-elseif statements. Sometimes it's cleaner code to do it one way or the other, but you can always achieve the same logical result by either syntax. As ReverendDexter said, a do-while block always executes at least once, but you can achieve the same result by writing the block once outside the loop, followed by a while block.

Posted: Fri Jun 29, 2007 11:30 am
by Gente
I can provide 1 advantage.
If we look at the relation between count of loop iterations and count of while condition verifications we will have following:
1. n to n+1 for while
2. n to n for do..while
But I don't really think this advantage is important.
Actually seems do..while is just anachronism that came from C.

Posted: Fri Jun 29, 2007 11:39 am
by RobertGonzalez
Multiple result set fetching from databases is one area that I use that logic.

Posted: Fri Jun 29, 2007 11:45 am
by Gente
Everah wrote:Multiple result set fetching from databases is one area that I use that logic.
Do..while? Can you give an example?

Posted: Fri Jun 29, 2007 11:55 am
by RobertGonzalez

Code: Select all

<?php
if ($mysql->multi_query($sql)) 
{
    $show_results = true;
    $rs = array();
        
    do {
        // Lets work with the first result set
        if ($result = $mysql->use_result()) 
        {
            // Loop the first result set, reading it into an array
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) 
            {
                $rs[] = $row;
            }
            
            // Close the result set
            $result->close();
        }
    } while ($mysql->next_result());
}?>

Posted: Fri Jun 29, 2007 12:30 pm
by superdezign
I used to use do...while loops in linked lists, but I trashed them in favor for the power of for loops.

Code: Select all

for($currentNode = $list->firstNode, $i = 0; $currentNode->next != NULL && $i >= 1; $currentNode = $currentNode->next, $i++)
$i keeps track of the list length (very useful on trees with children), and ensures that the loop runs at least once.

Posted: Fri Jun 29, 2007 12:36 pm
by idevlin
I've always used for loops, even in Java I shun the foreach command simply because I am so used to for loops from programming for years in C. And I too never had a use for do...while, always used for's.

Posted: Fri Jun 29, 2007 1:02 pm
by superdezign
idevlin wrote:I've always used for loops, even in Java I shun the foreach command simply because I am so used to for loops from programming for years in C. And I too never had a use for do...while, always used for's.
Whoa whoa there, hold your horses. foreach is awesome! Every language should have it! Without it, associative arrays would be that much harder to deal with. I'm as used to C++ as the next guy, but once I had the foreach I'm like, "ingenius!"

Posted: Fri Jun 29, 2007 1:10 pm
by idevlin
superdezign wrote:
idevlin wrote:I've always used for loops, even in Java I shun the foreach command simply because I am so used to for loops from programming for years in C. And I too never had a use for do...while, always used for's.
Whoa whoa there, hold your horses. foreach is awesome! Every language should have it! Without it, associative arrays would be that much harder to deal with. I'm as used to C++ as the next guy, but once I had the foreach I'm like, "ingenius!"
Oi! I never said there was anything wrong with it, it just never occurs to me to use it, I automatically write for loops instead.

Posted: Fri Jun 29, 2007 1:43 pm
by RobertGonzalez
foreach() is my friend. each(), not so much, but only because I found out the hard way that each() does not reset the array point while foreach() does.