Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It does not make any difference to the value of <code>col</code> within the loop - assuming <code>col</code> is a primitive value. If <code>col</code> was a class, the prefix and postfix '++' operators might be overloaded to do two different things, although I would consider it bad practice. Consider the following example:</p> <pre><code>#include &lt;iostream&gt; using namespace std; int main() { for(int i = 0; i &lt; 10; i++) { cout &lt;&lt; i &lt;&lt; endl; } cout &lt;&lt; endl; for(int i = 0; i &lt; 10; ++i) { cout &lt;&lt; i &lt;&lt; endl; } } </code></pre> <p>Both of these just print out 0 to 9, despite the fact that you pre-increment in one, and post-increment in the other. The incrementation of <code>i</code> happens at the end of each run of the loop whether or not you use pre or post increment. I believe pre-incrementing is more efficient, since - and I may be wrong here - the compiler does not then need to use a temporary variable<sup>1.</sup>, but this would only be noticeable if you are looping for a very long time (and of course <a href="http://en.wikipedia.org/wiki/Program_optimization#Quotes" rel="noreferrer">'More computing sins are committed in the name of efficiency than for any other single reason'</a>.)</p> <p>As for question 2:</p> <blockquote> <p>Question 2 : Would using >>= 1U instead of =/2 make any difference ?</p> </blockquote> <p>Unlikely. Bit shifting would be faster if the compiler did not optimise, but chances are that your compiler will optimise this into a bit shift.</p> <p>As a side note, I generally find doing <code>unsigned variableName</code> (that is, dropping the <code>int</code>) bad practice - although C++ will shove in an <code>int</code> anywhere one is missing, it is less readable to me.</p> <p><strong>1.</strong>: Stephen in the comments (a <em>different</em> Stephen ;) ) notes that - "Pre-increment is more efficient for standard library container iterators, but it's no different for primitive types, since copying an integer is cheaper than copying a larger iterator (in particular std::set and std::map iterators)."</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. 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