Increment and decrement confusion... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Increment and decrement confusion...

a = 5; b = 6; a = b++; c = ++a; document.write(a+c); //output : 14 ? why : not 13?

1st Aug 2021, 11:46 AM
Coder
Coder - avatar
4 Answers
+ 2
a = 5 b = 6 a = b++ (a = 6, b = 7) c = ++a (c = 7, a = 7) a + c = 14 If the plus signs are before the varible then it adds one before considering the rest of the equasion, if it is after, it adds one after the equation is done
1st Aug 2021, 11:50 AM
Brain & Bones
Brain & Bones - avatar
+ 3
a=b++ a=6 c=++a c=7 and a=7 as well
1st Aug 2021, 11:48 AM
Abhay
Abhay - avatar
+ 2
After a=b++; A becomes 6 and b becomes 7 After c=++a; A becomes 7 before assigning value to c, so c is 7 too. (7+7=14)
1st Aug 2021, 11:50 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
1st Aug 2021, 11:52 AM
Coder
Coder - avatar