Thread: Do while / while

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Do while / while

    Can sombody please explain to me who is correct in these statments:

    MY BOOK SAYS::

    Using Do and while statments are a better way to achieve your goal as they are better than while loops alone.

    MY TUTOR AT UNI SAYS::

    Always use a While loop WITHOUT do, as a while loop can always be found near the top of a program, as you do not have to go searching through the program to find it.

    Who is correct?? Please help I am confused.... Do i use while or Do while??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    a better way to achieve your goal
    What's your goal?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Depends on what you want to achieve.
    I've never had any practical use of a do-while loop (doesn't mean they're useless though).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    The goal would be to make the program execute better, but they are just two reasons for a function "while" that would really achieve the same thing, I was wondering why a book would say use DO WHILE and my proffessor say always only use WHILE

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    The only difference is that the expression is checked at different points of the block. Use whatever you need to. Most of the time you will find that while() will be used more but maybe a do-while() will be needed. One is not better than the other.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The goal would be to make the program execute better
    Um, like what kind of problem are you trying to solve with the loop? Why not a for loop?
    Why use a loop at all?

    I was wondering why a book would say use DO WHILE and my proffessor say always only use WHILE
    From what you say, it looks like your book and your prof are talking in different contexts.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    I don't like do-while loops only because it executes the code no matter what at least once and then checks the condition.

    But then again, there's probably some good use for that.
    Last edited by dra; 07-23-2005 at 03:33 AM.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    14
    One example where a do{ ... }while(); loop was useful for me was when I needed to create a random string which wasn't already present in a database.

    So,

    do {
    //generate string
    //query database looking for that string
    } while(string_was_found_in_db);

    basically this loop will never run more than once, but I needed to make sure a unique string was generated.

    However, do-while and while loop translated in asm (correct me if I'm wrong):
    do-while
    Code:
    startloop:
     ;
     ; loop body
     ;
     CMP x,x
     JNZ startloop
    while
    Code:
    startloop:
     CMP x,x
     JGE endloop
     ;
     ; loop body
     ;
     JMP startloop
    endloop:

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There are in general 3 types of loop. Theirs counted, top-tested and bottom-tested.
    for loops are counted.
    while loops are top-tested.
    do-while loops are bottom-tested.

    Just use whichever suits your needs best.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User c0ldshadow's Avatar
    Join Date
    Jun 2005
    Posts
    1
    if you want to get technical you could throw in a loop involving "goto", even tho they are highly frowned upon

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    I use do-while for my menu choice cause it makes sense to me.
    Code:
    int c;
    do
    {
         system("cls");
         cout<<"1- menu1\n";
         cout<<"2- menu2\n";
         cout<<"3- menu3\n";
         cout<<"4- quit\n";
         cin>>c;
         ... 
    
    }while(c!=4)
    to me it seems more sense.


    ofcourse you can always go

    Code:
    int c=-1;
    while(c!=4)
    {
        ...
        cin>>c;
    }

    whichever style u like or needed to get things done.

  12. #12
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    I believe hoangvo gave the best example here. Do while's are most useful when you have a loop that you want to execute at least once, usually with user input.

    This way the user input will always execute at least once, and you then check the user input at the while part of the do..while.

  13. #13
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    My Teacher asks me to use forloop when ever possible...
    Hi...Hi...Hi...

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you should probably favour while for general loops. for when you need a specific number of counted iterations and do/while when you need the loop body to execute before the looping test. Its that simple.
    Oh btw goto is not really a loop, its an unconditional jump. So even tho loops can be simulated using goto I would never include it in any discussion about loops.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Isn't for loop better.
    Every thing like intialisation, condition, incrementation... etc is packed into a neat little capsule... easier to understand right?
    btw, I'm addicted to for loops

Popular pages Recent additions subscribe to a feed