title: condition of for-loop only takes effect after hitting breakpoint
body: <p>I'm trying to solve a problem from hackerrank.</p>
  
  <p>At the moment my code is bad and wrong, but that's not the point of this question.
  The problem is the <code>for</code> loop, whose condition doesn't always seem to work.
  Here's the code:</p>
  
  <pre><code>#include &lt;cassert&gt;
  #include &lt;iostream&gt;
  #include &lt;set&gt;
  
  using namespace std;
  
  // Integer power.
  long ipow(int x, int y)
  {
      assert(x &gt; 0);
      assert(y &gt;= 0);
  
      long res = 1;
      while (y--)
          res *= x;
      return res;
  }
  
  std::set&lt;int&gt; compute_distinct_sequence_numbers(int n, int s, int p, int q)
  {
      static_assert(sizeof(long) &gt; 4, "long is assumed to be larger 4 bytes");
  
      set&lt;int&gt; distinct_numbers;
      const long max1 = ipow(2, 31);  // maximum plus 1
  
      // Compute first number of sequence.
      int prev = s % max1;
      distinct_numbers.emplace(prev);
  
      // Compute subsequent elements of sequence and put them in set.
      for (int i = 1; i &lt; n; ++i) {
          prev = (prev * p + q) % max1;
          distinct_numbers.emplace(prev);
      }
  
      return distinct_numbers;
  }
  
  int main(int argc, char** argv)
  {
      // Read numbers.
      int n, s, p, q;
      if (!(cin &gt;&gt; n &gt;&gt; s &gt;&gt; p &gt;&gt; q))
          throw std::runtime_error("error on input");;
  
      // Compute set containing distinct number of sequence.
      std::set&lt;int&gt; distinct_numbers = compute_distinct_sequence_numbers(n, s, p, q);
  
      // Print number of distinct numbers in sequence.
      cout &lt;&lt; distinct_numbers.size() &lt;&lt; std::endl;
  
      return 0;
  } 
  </code></pre>
  
  <p>If I run the program with the input "10000000 658061970 695098531 1430548937", it takes a very long time to complete (almost 20 seconds).</p>
  
  <p>The main problem I have with this, is that the loop doesn't exit when <code>i</code> reaches <code>n</code>.
  When I stop the program in GDB, the program is stuck in the loop and <code>i</code> may have a value larger than <code>n</code>.</p>
  
  <p>The following GDB session demonstrates this:</p>
  
  <pre><code>(gdb) run
  Starting program: /home/janosch/programming/hackerrank/cpp/main 
  10000000 658061970 695098531 1430548937
  
  Breakpoint 1, compute_distinct_sequence_numbers (n=10000000, s=658061970, p=695098531, q=1430548937) at main.cpp:32
  32              prev = (prev * p + q) % max1;
  (gdb) print i
  $7 = 10000000
  (gdb) info breakpoints 
  Num     Type           Disp Enb Address            What
  1       breakpoint     keep y   0x00000000004010c0 in compute_distinct_sequence_numbers(int, int, int, int) at main.cpp:32
          stop only if i = n
          breakpoint already hit 1 time
  (gdb) n
  33              distinct_numbers.emplace(prev);
  (gdb) 
  31          for (int i = 1; i &lt; n; ++i) {
  (gdb) 
  36          return distinct_numbers;
  (gdb) 
  37      }
  </code></pre>
  
  <p>I've set a conditional breakpoint (<code>i == n</code>) on the line 
  <code>prev = (prev * p + q) % max1;</code>.
  Normally it should not be possible to hit this breakpoint, since the loop should exit before hitting <code>n</code>.
  Even if I use a much higher value for the conditional breakpoint (like <code>i == 2*n</code>), the breakpoint is hit.</p>
  
  <p>Once I hit the breakpoint, the program exits the loop immediately. This is true for single-stepping (GDB command <code>n</code>) and continuing (GDB command <code>c</code>).</p>
  
  <p>Here is my super simple Makefile:</p>
  
  <pre><code>CC = g++
  CFLAGS = -Wall -std=c++14 -c -g
  LDFLAGS = 
  
  all:    main
  
  main:   main.o
          $(CC) $(LDFLAGS) main.o -o main
  
  main.o: main.cpp
          $(CC) $(CFLAGS) main.cpp
  </code></pre>
  
  <p>The program is running on Ubuntu 16.04.</p>
  
  <p>What am I doing wrong?
  How can the program behave differently when running in GDB?
  This seems to be embarrassingly simple.</p>