++ symbols

What does ++ signify? Would 24++ be 25 or 26?
the ++ signifies addition of one to the integer , the placement of the ++ determines whether or not the additon will occur before or after the expression is run.

for (a=1; a<=10; a++)

will add an integer of 1 each time the loop cycles until it gets to 10 (after the first cycle is run)

for (a=1; a<=10; ++a)
in this loop the integer is added before the loops first cycle,

24++ would be 25
Last edited on
Refer to your similar thread about the *= operator and Webjose reply.

++ will increment a variable by 1 - it actually modifies the variable
(your example above is invalid because you cannot modify the value 24).

++ is used in pre-increment and post-increment modes - it can have undefined behaviour especially when used in post-increment mode.
it can have undefined behaviour especially when used in post-increment mode.
Neither form is particularly susceptible to undefined behavior. The results of both (a+ ++a) and (a+a++) are undefined because a variable being incremented or decremented is being used more than once in the same expression.

for (a=1; a<=10; a++)
and
for (a=1; a<=10; ++a)
have the exact same meaning*. Any for loop with the structure for (A;B;C){D} can be converted to the following while loop regardless of A, B, C, or D:
1
2
3
4
5
6
7
{
    A;
    while (B){
        D
        C;
    }
}


* Well, almost. A compiler is not required to recognize that the intermediate value of a++ is not being used, making the expression equivalent to ++a, so a++ may actually generate a bit more code on some compilers.
the loops do have the same meaning, but the ++a's output will start at 2 and loop 1 less time than the a++ of course, so they are different in that respect. as far as the 24++ being 25, i was just answering the original question generally,my example was just to demonstrate the plus 1 process, i could have used a different example to cause less confusion
BettyBoopTS wrote:
the loops do have the same meaning, but the ++a's output will start at 2 and loop 1 less time than the a++ of course


No, they won't.

for(int i = 0; i < 10; ++i)
and
for(int i = 0; i < 10; i++)
both loop 10 times.

Look at helios's equivalent while loop and plug in A, B, and C.
* Well, almost. A compiler is not required to recognize that the intermediate value of a++ is not being used, making the expression equivalent to ++a, so a++ may actually generate a bit more code on some compilers.



For real? that's silly, I took for granted that a++ would always be a post increment. In fact a number of function I have use a++ and would break using ++a
It only matters if the result of the ++ operator is being used elsewhere on the line.

For example:

1
2
3
4
5
6
7
8
9
int a = 1;
int b = ++a;
 // a=2
 // b=2

a = 1;
b = a++;
 // a=2
 // b=1 


If you are not passing b++ to a function, assigning it to a variable, or doing anything else with it other than incrementing b, then it doesn't really matter* as both pre and post operators have the same impact on b.


* Where it does matter is when 'b' is a object that has the ++ operators overloaded. In that event, which ++ you use can call two entirely different functions, and the performance difference of those functions can vary. Typically, the post (b++) requires an object copy(-ies) and is therefore less efficient. But that's not always the case.

If the ++/-- operators are inlined, then it generally doesn't matter because (I'd imagine) most compilers will optimize out the unnecessary copies. So for stuff like STL iterators, it probably doesn't matter, since that stuff is probably all inlined.

If they're not inlined though, it can make a difference, because there's no way for the compiler to optimize out the copy(-ies) since its done in the function, so there's no way it can know that its unnecessary.


In any event... I recommend getting in the habit of using the prefix form (++a), since it'll never* yield worse performance and might yield better performance sometimes. Unless of course you need the postfix form because you're combining it with an assignment or something -- but ew @ compound operations.


* (it would be possible to write a prefix form that is less efficient than the postfix form, so I can't say 'never' here and really mean it -- but in all properly written prefix/postfix operator overloads, the prefix form would be preferred)


EDIT:

also, helios and firedraco are right, of course. in the above mentioned loops, there is no difference between using ++a or a++. Both loops will have a start and stop with the same values.
Last edited on
For real? that's silly, I took for granted that a++ would always be a post increment. In fact a number of function I have use a++ and would break using ++a
I think I phrased it ambiguously. This is what I meant:
1
2
3
4
//...
a++;
++a;
//... 

Because the intermediate values are not being used, lines 2 and 3 are equivalent, and would give the same result if swapped. The compiler can take advantage of this situation for optimization by changing a++ to ++a, which is always faster. Needless to say, this can't be done in any of these cases:
1
2
3
b=a++;
b=a+++c;
f(a++);

because that would change the result of the operation. The optimization can only be done when it's the last thing that will be executed in an expression.

Here's another situation where using increment or decrement operators has undefined behavior:
f(a++,a++);
thanks for the explanations, it cleared things up for me, i stand corrected. I shouldve wrote the expression as follows as the example;


1
2
3
4
5
6
7
8
9
10
11
int main()
{
int a,b;
b=24;
for (a=1; a<=10; a++) {b++; cout<<b <<"\n";}


return 0;
}

Topic archived. No new replies allowed.