How does ++i and i++ works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How does ++i and i++ works?

I was doing a quiz and got confused with this question, i answered wrong, whats wrong int i=5; int j=i++; int k=j++; Console.Write(k+i) ; Please explain if u can

10th Dec 2019, 10:04 PM
Mateusz Legend
Mateusz Legend - avatar
42 Answers
+ 18
++a - increments and then uses the variable a++ - uses and then increments the variable. int x = 5; int y = ++x; // y = 6 int x = 5; int y = x++; // y = 5
11th Dec 2019, 7:25 AM
Dawn Pal
+ 5
++i increments the value before the end of the statement whereas i++ does that after the execution of that statement ie. The value of i in i++ will remain same as i in that statement
12th Dec 2019, 6:56 AM
Jolin Thomas
+ 4
Int n1 = 1, n2 = 2; Int n3, n4; n3 = n1++; // n3 = 1, n1 = 2 n4 = ++n2; // n4 = 3, n2 = 3 // Same procedure for minus //but with a decrement
11th Dec 2019, 7:56 PM
ihab 🌼
ihab 🌼 - avatar
+ 3
++i will change i's value when it goes to next line while i++ will change the value of i from the same line.
12th Dec 2019, 8:17 AM
Alex Kuriakose
Alex Kuriakose - avatar
+ 2
If it was 12 i would not Even ask. The question is why it is 11 :D
11th Dec 2019, 5:20 AM
Mateusz Legend
Mateusz Legend - avatar
+ 2
I guess so. int i=5; int j=i++; int k=j++; Console.Write(k+i); So now my i is 6, j is 6 and k is 5.
11th Dec 2019, 5:42 AM
Mateusz Legend
Mateusz Legend - avatar
+ 2
Steps: 1. i=5. 2. j=5, i=6. 3. k=5, j=6. 4. Write (5+6) // answer: 11
11th Dec 2019, 6:05 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Yep now i got it thanks ;>
11th Dec 2019, 6:10 AM
Mateusz Legend
Mateusz Legend - avatar
+ 2
x-= means x= x-x so if x is 7 the answer is 14 because x = 7 - (-7) = 7+7 = 14
11th Dec 2019, 6:21 AM
Mateusz Legend
Mateusz Legend - avatar
+ 2
++i means add 1 before using it i++ means add 1after its first use
11th Dec 2019, 4:04 PM
Chandu187
Chandu187 - avatar
+ 2
i++ means increment before using But ++ i means after one use increment is applied
12th Dec 2019, 4:38 AM
Chandu187
Chandu187 - avatar
+ 2
X=6 X++ Then x=6 ++X Then X=7
12th Dec 2019, 5:27 AM
Chandu187
Chandu187 - avatar
+ 2
Here in that question i=5 J=i++ then j=5 K=j++ Then j=6 k+i=11
12th Dec 2019, 5:30 AM
Chandu187
Chandu187 - avatar
+ 2
++X means pre increment so we increased it by one so x=7 explain will you know it better
12th Dec 2019, 5:33 AM
Chandu187
Chandu187 - avatar
+ 2
If operator first so do it which + second you can do what you want Which may be assign or compare and vice versa
12th Dec 2019, 6:25 AM
Mahmoud Salama Sleem
+ 2
Here simple logic to understand the concept,don't get confused. •Before implemention => ++a •After implementation=> a++ Lesson 1=> If the increment is after the variable (like a++) then the incrementing variable by one in next attempt of the statement •For example=> int a; //undefined a++;// a is not incremented by one console.log(a);//a is incremented by one and logged out a=1; •One more Example=> for(int a=0; a<=1;a++);//a is declare and initialized by 0 and condition satistified as true and a is not incremented by one instead a is increment next iteration of the for-loop sequence In-brief => •Iteration 1: a=0;a<=1;a=0;//instead of a=1 by a++ •iteration 2: a=1;a<=1;a=1;//previously a=0 and a is incremented by one with the help of a++ by next attempt of the statement •iteration 3: a=2;a<=1;a++;//loop stops by condition false(2<=1) and value of a =2 Lesson 2=> If the increment is before the variable (like ++a) then the implementing variable by one is done at that point of execution of that statement
12th Dec 2019, 7:49 AM
Vinay Govardhanam
Vinay Govardhanam - avatar
11th Dec 2019, 4:22 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
do you understand what is prefix and Postfix variable entry? what is their difference?
11th Dec 2019, 5:30 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
And how much in this: x=7 x -= -x
11th Dec 2019, 6:12 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar