Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're trying to bring a python idiom into C++. That's unncessary. Use </p> <pre><code>for(int i=initVal;i&lt;range;i+=increment) { /*loop body*/ } </code></pre> <p>to achieve this. In Python, the <code>for(i in xrange(init, rng, increment))</code> form is necessary because Python doesn't provide a simple for loop, only a for-each type construct. So you can iterate only over a sequence or a generator. This is simply unnecessary and almost certainly bad practice in a language that provides a <code>for(;;)</code> syntax.</p> <p>EDIT: As a completely non-recommended aside, the closest I can get to the <code>for i xrange(first, last, inc)</code> syntax in C++ is:</p> <pre><code>#include &lt;cstdio&gt; using namespace std; int xrange(unsigned int last, unsigned int first=0, unsigned int inc=1) { static int i = first; return (i&lt;last)?i+=inc:i=0; } int main() { while(int i=xrange(10, 0, 1)) printf("in loop at i=%d\n",i); } </code></pre> <p>Not that while this loops the correct number of times, i varies from <code>first+inc</code> to <code>last</code> and NOT <code>first</code> to <code>last-inc</code> as in Python. Also, the function can only work reliably with <code>unsigned</code> values, as when <code>i==0</code>, the <code>while</code> loop will exit. Do <em>not</em> use this function. I only added this code here to demonstrate that something of the sort is indeed possible. There are also several other caveats and gotchas (the code won't really work for first!=0 on subsequent function calls, for example)</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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