Difference between while and do while

Discussion in 'C Programming' started by Logan Lee, Dec 24, 2007.

  1. Logan Lee

    Logan Lee Guest

    Hi. What's the difference between while and do while?
    Logan Lee, Dec 24, 2007
    1. Advertisements

  2. Logan Lee

    Dinesh P Guest

    In While loop the condition is tested first and then the statements are
    executed if the condition turns out to be true.
    In do while the statements are executed for the first time and then the
    conditions are tested, if the condition turns out to be true then the
    statements are executed again.

    A typical scenario to use do While loop.
    I would like to get a specified input from user. Here first I will get the
    input then I will check whether we got the specified input other wise we
    will again ask for the input.
    char input;
    printf("say yes or no :(y/n)";
    input = getchar();
    }while(!(input == 'y' || input == 'n'));
    Dinesh P, Dec 24, 2007
    1. Advertisements

  3. Logan Lee

    Ian Collins Guest

    Where do you see the test?
    Ian Collins, Dec 24, 2007
  4. What does your textbook say?

    I'm not trying to be unhelpful. This is very elementary question,
    something that any decent textbook or tutorial should answer.
    Keith Thompson, Dec 24, 2007
  5. Logan Lee

    Eric Sosman Guest

    Are you the same "Logan Lee" who's lecturing us all on
    how to do static code analysis? If you're going to analyze
    code, wouldn't it be a good idea to learn the language first?

    Get a C textbook. Usenet is a fine way to transmit some
    kinds of knowledge, but it's not well suited to delivery of
    "the 101 course."
    Eric Sosman, Dec 24, 2007
  6. Logan Lee

    osmium Guest

    A do while is used for a block of code that must be executed at least once.
    These situations tend to be relatively rare, thus the simple while is more
    commonly used.
    osmium, Dec 24, 2007
  7. The best way to express this is that:

    do {<statement>} while (<expr>);

    is equivalent to:

    while () do {};

    i.e., do it once, then enter into a normal "while" loop.
    Kenny McCormack, Dec 24, 2007
  8. Logan Lee

    santosh Guest

    Yes. In C syntax that would be:

    { };
    while () { }

    You could also place the loop at the bottom of the previous block.
    santosh, Dec 25, 2007
  9. Logan Lee

    Ben Pfaff Guest

    The semicolons and the "do" keyword should be dropped (and some
    content should be added to the parentheses and braces).
    Ben Pfaff, Dec 25, 2007
  10. Logan Lee

    James Kuyper Guest

    You've repeatedly asked this newsgroup very elementary questions about
    C. Would you please bother reading an elementary C textbook to find out
    the answers to these questions? If you've already done so, and you still
    have this many questions about matters this elementary, may I politely
    suggest that computer programming doesn't seem like a good career choice
    for you?
    James Kuyper, Dec 25, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.