title: C++ dynamic nested for loop
body: <p>I am trying to compute the following quantity with a dynamic number of loops. The pseudo-code looks like</p>
  
  <p>When k = 1,</p>
  
  <pre><code>for (x1 =0;x1&lt;=nmax[1];x1++){
      a = a + x1 * (x1&gt;upper[1]);
  }
  </code></pre>
  
  <p>When k = 2</p>
  
  <pre><code>for (x1 =0;x1&lt;=nmax[1];x1++){
      for (x2 =0;x2&lt;=nmax[2];x2++){
          a = a + x1 * (x1&lt;upper[1]) * x2 *(x1+x2&gt;upper[2]);
      }
  }
  </code></pre>
  
  <p>When k = 3</p>
  
  <pre><code>for (x1 =0;x1&lt;=nmax[1];x1++){
      for (x2 =0;x2&lt;=nmax[2];x2++){
          for (x3 =0;x3&lt;=nmax[3];x3++){
              a = a + x1 * (x1&lt;upper[1]) * x2 *(x1+x2&lt;upper[2]) * x3 *(x1+x2+x3&gt;upper[3]);
          }
      }
  }
  </code></pre>
  
  <p>nmax and upper are predefined vector.</p>
  
  <p>To illustrate the logic behind the boolean expressions, as an example, as k increases, the boolean expression develops as follows. For the first one to the second before the last, it's &lt;; whereas the last one uses >.</p>
  
  <pre><code>(x1&gt;upper[1])
  (x1&lt;upper[1]) AND (x1+x2&gt;upper[2]) 
  (x1&lt;upper[1]) AND (x1+x2&lt;upper[2]) AND (x1+x2+x3&gt;upper[3])
  (x1&lt;upper[1]) AND (x1+x2&lt;upper[2]) AND (x1+x2+x3&lt;upper[3]) AND (x1+x2+x3+X4&gt;upper[4])
  (x1&lt;upper[1]) AND (x1+x2&lt;upper[2]) AND (x1+x2+x3&lt;upper[3]) AND (x1+x2+x3+x4&lt;upper[4]) AND (x1+x2+x3+x4+x5&gt;upper[5])
  </code></pre>
  
  <p>Is there a way to write a function that use k as an argument to achieve the above?</p>