Note that there are some explanatory texts on larger screens.

plurals
  1. PODetect first pass through a do-while
    primarykey
    data
    text
    <p>I have a do-while loop that needs to log a message once (so it doesn't clutter the log) each time its status (e.g. pass/fail) changes, but still has to do other things each time it goes through the loop. Using a simple boolean variable can basically tell you if you've already logged that message, which works once you're in a known condition. However, if you want the message to be printed the first time in either case (pass/fail), you have to account for that. For example, if you default your condition to <code>true</code>, and it is, in fact, true the first time, it won't log the 'True' message b/c it thinks it was already true (and vice-versa for i.c. false).</p> <p>This seems like it would be a good place for a <a href="http://en.wikipedia.org/wiki/Nullable" rel="nofollow">nullable</a> boolean with i.c.=Null, but in languages where those aren't present, what's one to do?</p> <p>The simplest solution I could think of would be to use an extra boolean variable like 'firstTime = True', but using that always bothers me as an elementary workaround when I feel like there should be a more delicate way to handle it. Another option is to use the breakout condition of the do-while as your initial condition for whatever variable you're using as your conditional, but that can be confusing when someone reads <code>int status = STATUS_QUIT</code>, and it certainly requires more explanatory comments than <code>bool firstTime = true</code>. A third option would be to use an enum instead of a bool and have {firstTime, true, false} or something.</p> <p>Are there other reasons for using one over the other, or are there better ways of doing this?</p> <p>Code example with two options I came up with:</p> <p><strong>Using <code>bool firsttime</code>:</strong></p> <pre><code>bool firstTime = true, infoFound = false; do { if (getInfo()) { if (!infoFound) { // log it (ONCE)(important) infoFound = true; } // use info (every time) } else if (infoFound || firstTime) { // log it (ONCE)(important) infoFound = false; firstTime = false; } // FYI, WaitForStatusUpdate is a blocking call... } while (STATUS_QUIT != WaitForStatusUpdate()); </code></pre> <p><strong>Use the <code>while</code> loop 'break-out condition' as the initial condition for a check variable:</strong><br> (<code>status</code> is updated at the end of the do-while, so the <code>do</code> section will not be executed ever again if <code>status == breakOutCondition</code>; we <em>can</em> use this to our advantage here and set <code>status = breakOutContition</code> initially - the first time through it will be <code>breakOutCondition</code> but any subsequent loop <em>will be</em> something else... Still not sure I like this as it's kind of a hack...</p> <pre><code>bool infoFound = false; int status = STATUS_QUIT; do { if (getInfo()) { if (!infoFound) { // log it (ONCE)(important) infoFound = true; } // use info (every time) } else if (infoFound || firstTime) { // log it (ONCE)(important) infoFound = false; } status = WaitForStatusUpdate(); } while (STATUS_QUIT != status); </code></pre> <p>(I'm tagging this as c++ since that's what I'm using, but this really could apply to any language with similar constructs)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload