Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I'm in the i++ camp, and it doesn't matter in C anyway.
However, it may matter in C++ if i isn't a POD and the adding operator is overloaded, then ++i has the potential to be faster so that it should be preferred in general. -
iAmNaN68456yUm, really? ++i is pre-increment and i++ is post-increment, and are used in different situations. Pre-increment increments the value before it is used. Post increment increments the value after the expression completes. Because of this, the outcomes will be different. There are distinct cases in which each is used.
-
Performancewise, ++i is better. It increments i and returns it. i++ copies i, returns it and then increments original iirc.
-
Pre-increment for performance-critical code and by default as post-increment has to make a copy of i and retain it which is a little overhead and to me ++i does make it more clear what’s going on in a longer arithmetic expression for example because you don’t have to keep track of i’s state.
-
you guys want to see something scary?
$i = 0;
while ($i < 5) do {
// some shit
$i = $i + 1;
} -
Flygger19796yFor those citing performance, you really should restructure you loops to run backwards instead; referencing the end value as initialisation (especially for something like length) and then counting down comparing with zero will be much more performant than switching increment operator.
Also, I prefer code that's easier to read and change, which makes me go for the `i += 1` style ;) -
@PonySlaystation oh the Bait question is a master.
I prefer B. But it is a case to case basis. We need context 😀
Bait question: What's your style?
a) for ( ... ; ++i ) {
b) for ( ... ; i++ ) {
I am disgusted of b). a) for the win 😎
question
ww3