--- In [email protected], Asad Abbas <cyberstuden...@...> wrote:
>
> Why these both statements have same output??
> why i++ and ++i works alike in for loop?? 
> 
>  
> for(int i=0;i<5;i++) 
> or 
> for(int i=0;i<5;++i);
>    cout << i << endl;
> 
> 
> //output:
> /*
> 0
> 1
> 2
> 3
> 4
> */

For language native types the compiler generates the same code in this case for 
either post- or pre- increment. So you would expect the same output.

However, in C++ where you might be using a user-defined type, the prefix form 
can me more efficient because it does not need to return the original value. 
For this reason in C++ it is recommended (Sutter, _C++ Coding Standards_; 
Meyers, _Effective C++_) that you get into the habit of using the prefix form 
in all cases where you don't need the original value (as in loop counters), to 
avoid a "premature pessimization".


Reply via email to