Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postfix and prefix increment operator in a for loop [duplicate]

Tags:

Possible Duplicate:
Difference between i++ and ++i in a loop?

Can anyone explain what's the difference between those:

for(unsigned col = 0; col < n; ++col, num_to_fill >>= 1U) {      for(unsigned row = num_to_fill; row < (1U << n); row += (num_to_fill * 2))     {         std::fill_n(&output[col][row], num_to_fill, 1);     } } 

and

for(unsigned col = 0; col < n; col++, num_to_fill >>= 1U) {      for(unsigned row = num_to_fill; row < (1U << n); row += (num_to_fill * 2))     {         std::fill_n(&output[col][row], num_to_fill, 1);     } } 

When col=0 , In ex.1 Output[col][row] will be output[1][row] and In ex.2 Output[col][row] will be output[0][row] . Am I right ?

Question 2 : Would using >>= 1U instead of /= 2 make any difference ?

like image 925
Ahmed Avatar asked Aug 22 '10 12:08

Ahmed


People also ask

How does pre increment and Post increment work in for loop?

Pre increment directly returns the incremented value, but post increments need to copy the value in a temporary variable, increment the original and then returns the previous made copy.

Should I use i ++ or ++ i in for loops?

Both increment the number, but ++i increments the number before the current expression is evaluted, whereas i++ increments the number after the expression is evaluated. To answer the actual question, however, they're essentially identical within the context of typical for loop usage.

Is i ++ the same as ++ i?

i++ is known as post increment whereas ++i is called pre increment. i++ is post increment because it increments i 's value by 1 after the operation is over. Here value of j = 1 , but i = 2 . Here the value of i will be assigned to j first, and then i will be incremented.

How does increment work in for loop?

Increment is an expression that determines how the loop control variable is incremented each time the loop repeats successfully (that is, each time condition is evaluated to be true). The for loop can proceed in a positive or negative fashion, and it can increment the loop control variable by any amount.


1 Answers

It does not make any difference to the value of col within the loop - assuming col is a primitive value. If col 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:

#include <iostream>  using namespace std;  int main() {     for(int i = 0; i < 10; i++) {         cout << i << endl;     }      cout << endl;      for(int i = 0; i < 10; ++i) {         cout << i << endl;     }  } 

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 i 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 variable1., but this would only be noticeable if you are looping for a very long time (and of course 'More computing sins are committed in the name of efficiency than for any other single reason'.)

As for question 2:

Question 2 : Would using >>= 1U instead of =/2 make any difference ?

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.

As a side note, I generally find doing unsigned variableName (that is, dropping the int) bad practice - although C++ will shove in an int anywhere one is missing, it is less readable to me.

1.: Stephen in the comments (a different 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)."

like image 61
Stephen Avatar answered Sep 24 '22 05:09

Stephen