+ 3
What are the differences between ++i and i++ as incrementors of a for loop?
8 Answers
+ 10
--x decrements the value first and uses the new value in the statement.
x-- first executes the statement and decrements the value of x after that.
exampeles:
int x=1;
cout<<++x;//output is 2.
int y=1;
cout<<y++;//output is 1
but be careful in this example:
int x=1;
int y=3;
cout<<++x+y--+x;
the output will be 7.
why ?
first x changed to 2 .++x
second y will be same because we took the value of y before the decrement .y--
third : the second x will be 2 because it increased at first
so the answer is 2+3+2=7
+ 6
you can test that by using codes
https://code.sololearn.com/cmcM0rNvTl84/?ref=app
+ 2
https://www.sololearn.com/discuss/489143/?ref=app
+ 1
--decrements
Assigns than decreaments--
0
The ++i increments i before the operation while the i++ increments i after the operation.
Write and run a simple for loop with cout << i to compare the two.
0
++i is pre-increment, which means first increment then use it by 1.
i++ is pre-increment, which means first use then increment.
To understand it better, try to do a simple practical.
0
So, I found these two links:
https://www.quora.com/Do-you-use-i++-or-++i-in-a-for-loop-And-why?share=1 â General discussion of the question
https://stackoverflow.com/questions/484462/difference-between-i-and-i-in-a-loop/484492#484492 â Explanation
tl;dr: if i is something simple and not an iterator, it doesn't matter if you use ++i or i++ as increment in a for loop. But because it could be an iterator where operator++ is overloaded, it might be better to stick to ++i.
0
I can't get it can anyone describe ezly pls?