+ 3

What are the differences between ++i and i++ as incrementors of a for loop?

28th May 2017, 6:35 PM
Pacha
8 ответов
+ 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
28th May 2017, 6:56 PM
Enas Emad
Enas Emad - avatar
+ 6
you can test that by using codes https://code.sololearn.com/cmcM0rNvTl84/?ref=app
28th May 2017, 6:59 PM
Enas Emad
Enas Emad - avatar
+ 2
https://www.sololearn.com/discuss/489143/?ref=app
25th Jun 2017, 10:09 AM
Siddharth Saraf
+ 1
--decrements Assigns than decreaments--
29th May 2017, 2:34 AM
John 3:16
John 3:16 - avatar
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.
28th May 2017, 6:42 PM
Joseph P French
Joseph P French - avatar
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.
28th May 2017, 6:45 PM
TakshakSH
TakshakSH - avatar
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.
28th May 2017, 7:27 PM
Linus
0
I can't get it can anyone describe ezly pls?
5th Jun 2017, 5:15 AM
harsharan