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
    1. Advertising

  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.
    eg.,
    do
    {
    char input;
    printf("say yes or no :(y/n)";
    input = getchar();
    }while(!(input == 'y' || input == 'n'));


    "Logan Lee" <> wrote in message
    news:476f51a9$0$26179$...
    > Hi. What's the difference between while and do while?
     
    Dinesh P, Dec 24, 2007
    #2
    1. Advertising

  3. Logan Lee

    Ian Collins Guest

    Logan Lee wrote:
    > Hi. What's the difference between while and do while?


    Where do you see the test?

    --
    Ian Collins.
     
    Ian Collins, Dec 24, 2007
    #3
  4. Logan Lee <> writes:
    > Hi. What's the difference between while and do while?


    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 (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 24, 2007
    #4
  5. Logan Lee

    Eric Sosman Guest

    Logan Lee wrote:
    > Hi. What's the difference between while and do while?


    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
    lid
     
    Eric Sosman, Dec 24, 2007
    #5
  6. Logan Lee

    osmium Guest

    "Logan Lee" writes:

    > Hi. What's the difference between while and do while?


    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
    #6
  7. In article <>,
    osmium <> wrote:
    >"Logan Lee" writes:
    >
    >> Hi. What's the difference between while and do while?

    >
    >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.


    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
    #7
  8. Logan Lee

    santosh Guest

    Kenny McCormack wrote:

    > In article <>,
    > osmium <> wrote:
    >>"Logan Lee" writes:
    >>
    >>> Hi. What's the difference between while and do while?

    >>
    >>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.

    >
    > 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.


    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
    #8
  9. Logan Lee

    Ben Pfaff Guest

    (Kenny McCormack) writes:

    > {};
    > while () do {};


    The semicolons and the "do" keyword should be dropped (and some
    content should be added to the parentheses and braces).
    --
    Peter Seebach on C99:
    "[F]or the most part, features were added, not removed. This sounds
    great until you try to carry a full-sized printout of the standard
    around for a day."
     
    Ben Pfaff, Dec 25, 2007
    #9
  10. Logan Lee

    James Kuyper Guest

    Logan Lee wrote:
    > Hi. What's the difference between while and do while?


    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
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,279
  2. Santa
    Replies:
    1
    Views:
    1,095
    Mark A. Odell
    Jul 17, 2003
  3. Uday Bidkar
    Replies:
    4
    Views:
    498
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    367
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    272
    PerlFAQ Server
    Apr 15, 2011
Loading...

Share This Page