Thread: Do not understand this do..while loop

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Do not understand this do..while loop

    Hi everyone,

    Could someone explain me how does this do..while work? What is the purpose of while (!m_finished)? How the loop will break if while (0)?

    Code:
    #define WAIT_FOR_PERIPH() \
      do {                    \
        while (!m_finished) { \
        }                     \
        m_finished = false;   \
      } while (0)
    Thanks Nick

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    The outer loop seems useless. Seeing that the inner loop is some kind of busy loop that waits for the value of m_finished to change, and judging by the name WAIT_FOR_PERIPH, I'd say this snippet waits for some kind of user-defined interrupt to be triggered.

    It seems possible that the outer loop is there to prevent the compiler from optimizing away the inner one... I can't think of any other reason...

    EDIT: Oh yeah, I forgot about the semi-color! Disregard my answer, laserlight is right.
    Last edited by GReaper; 12-18-2020 at 01:11 PM.
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Since this is a function-style macro, the do while (0) loop here is used for the sole purpose of making the "function body" self-contained so that the macro could be used where a single statement was expected and behave in the same way. You could just as well change it to a pair of braces, except then the "caller" could leave out the terminating semi-colon.
    Last edited by laserlight; 12-18-2020 at 01:09 PM.
    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

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Sorry for the late response, thanks guys!!

    Ok, so the do..while(0) loop is actually used in order to make a uniform macro function, a function that it can be used anywhere especially in if..else statements and a semicolonafter the macro will always have the same effect.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by laserlight View Post
    You could just as well change it to a pair of braces, except then the "caller" could leave out the terminating semi-colon.
    You say "could" leave out the terminating semicolon, but you mean "must" leave out the terminating semicolon, or else the following will not work:
    Code:
        if (b)
            WAIT_FOR_PERIPH();  // the semicolon is an error with just braces
        else
            something_else();
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do not understand this infinite loop and why it'z used
    By Mad_hatter69 in forum C Programming
    Replies: 2
    Last Post: 03-08-2018, 02:56 PM
  2. Replies: 1
    Last Post: 08-31-2012, 12:13 PM
  3. Can someone help me understand this very simple loop?
    By matthayzon89 in forum C Programming
    Replies: 2
    Last Post: 04-17-2010, 09:12 AM
  4. trying to understand the message loop
    By bling in forum Windows Programming
    Replies: 4
    Last Post: 08-08-2008, 09:27 AM
  5. for loop again. I don't understand the construction.
    By cdalten in forum C Programming
    Replies: 6
    Last Post: 03-22-2006, 09:06 AM

Tags for this Thread