title: How to correctly exit a for loop and run the next conditional statement
body: <p>UPDATE: Thank you for the help. Writing a demo that you could run did help me solve the issue but not in the way that I expected. I think this is a compiler optimization rather than a bug. When the code inside if ( z == true ) is commented out, the conditional statement is skipped entirely and the control returns to if (x). If I put actual code in, we hit the conditional statement when appropriate.</p>
  
  <p>ORIGINAL QUESTION:
  I have a std::string and I am iterating through it to determine whether it contains certain characters. If any of these characters are found, I want to exit the for loop and proceed with the next conditional statement. Here is an example:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      bool x = true;
      bool y = true;
      bool z = true;
  
      std::string str;
  
      cout &lt;&lt; "Enter string: ";
      cin &gt;&gt; str;
  
      if ( x )
      {
          if ( y )
          {
              std::string::iterator i;
  
              for ( i = str.begin(); i != str.end(); i++ )
              {
                  cout &lt;&lt; "Enter for" &lt;&lt; endl;
  
                  if ( *i == 'a' || *i == 'b' || *i == 'c' )
                  {
                      z = false;
                      cout &lt;&lt; "Exit for" &lt;&lt; endl;
                      break;
                  }
              }
              if ( z == true )
              {
                  //cout &lt;&lt; "z == true" &lt;&lt; endl;
              }
          }
      }
  }
  </code></pre>
  
  <p>The issue is that the program never hits if (z == true). When it breaks out of the loop, control returns to the first conditional statement ( if (x) ).</p>
  
  <p>I tried removing the break and instead setting i = ( str.end() - 1 ). This resulted in the same behavior - it goes back to the for, determines that it's at the last character of the string, and then returns control to the first conditional statement again, skipping the if ( z == true ) as intended.</p>
  
  <p>What am I doing wrong here?</p>