title: C++: Set bool value only if not set
body: <p>I have code in my C++ application that generally does this:</p>
  <pre><code>bool myFlag = false;
  while (/*some finite condition unrelated to myFlag*/) {
      if (...) {
          // statements, unrelated to myFlag
      } else {
          // set myFlag to true, perhaps only if it was false before?
      }
  }
  if (myFlag) {
      // Do something...
  }
  </code></pre>
  <p>The question I have pertains to the <code>else</code> statement of my code. Basically, my loop may set the value of myFlag from false to true, based on a certain condition not being met. Never will the flag be unset from true to false. I would like to know what statement makes more sense performance-wise and perhaps if this issue is actually a non-issue due to compiler optimization.</p>
  <pre><code>myFlag = true;
  </code></pre>
  <p>OR</p>
  <pre><code>if (!myFlag) myFlag = true;
  </code></pre>
  <p>I would normally choose the former because it requires writing less code. However, I began to wonder that maybe it involved needless writing to memory and therefore the latter would prevent needless writing if myFlag was already true. But, would using the latter take more time because there is a conditional statement and therefore compile code using more instructions?</p>
  <p>Or maybe I am over-thinking this too much...</p>
  <hr>
  <h3>UPDATE 1</h3>
  <p>Just to clarify a bit...the purpose of my latter case is to not write to memory if the variable was already true. Thus, only write to memory if the variable is false.</p>