Notices
Results 1 to 5 of 5

Thread: PHP Do While Loop Questionary

  1. #1 PHP Do While Loop Questionary 
    Forum Ph.D. Steve Miller's Avatar
    Join Date
    Dec 2006
    Location
    Magdeburg, Saxony-Anhalt, Germany
    Posts
    782
    Hello everyone,

    how does this
    Code:
    $_c = 6;
            do {
                echo 'What the fu***!';
            }
            while ($_c < 5);
    echo 'What the fu***!' to the screen, although $_c > 5 right from the start of the code snippet?

    Steve


    Reply With Quote  
     

  2.  
     

  3. #2  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,489
    Because the test is at the end, a do-while loop always executes at least once. As opposed to a while loop, where the test is done before the loop body.


    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  4. #3  
    Forum Ph.D. Steve Miller's Avatar
    Join Date
    Dec 2006
    Location
    Magdeburg, Saxony-Anhalt, Germany
    Posts
    782
    Hi Strange,

    wasn't this do while structure useless then? Anyway, when I alter this code it does not execute.

    Steve
    Reply With Quote  
     

  5. #4  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,489
    Quote Originally Posted by Steve Miller View Post
    Hi Strange,

    wasn't this do while structure useless then? Anyway, when I alter this code it does not execute.

    Steve
    It is not useless but there aren't many cases where it is useful! I have used it for reading from a file, for example. You always want to do at least one read to see if you get data or an end of file.
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  6. #5  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    If you find yourself doing something like this:
    Code:
    doSomething();
    
    while(!done) {
        doSomething();
    }
    You could shorten that with a do-while.
    Code:
    do {
        doSomething();
    } while(!done);
    If you replace doSomething with more than a line or two of code, it can be significant. But I agree with Strange, it doesn't come up all that often.
    Reply With Quote  
     

Similar Threads

  1. Chiller Water Loop with Heat Exchanger and Contamination in the loop
    By funhere in forum Mechanical, Structural and Chemical Engineering
    Replies: 4
    Last Post: July 1st, 2012, 02:28 PM
  2. Loop-the-loop machine problem
    By Heinsbergrelatz in forum Physics
    Replies: 2
    Last Post: October 30th, 2010, 08:49 AM
  3. Php sources
    By DrakenHeart in forum Computer Science
    Replies: 4
    Last Post: December 22nd, 2008, 11:43 AM
  4. PHP devloper
    By fastcoder in forum Computer Science
    Replies: 0
    Last Post: March 8th, 2008, 04:39 AM
  5. PHP
    By weknowtheword in forum Computer Science
    Replies: 1
    Last Post: March 15th, 2007, 06:40 AM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •