Increment and decrement confusion... | Sololearn: Learn to code for FREE!

0

Increment and decrement confusion...

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

8/1/2021 11:46:24 AM

Coder

4 Answers

New Answer

+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

+3

a=b++ a=6 c=++a c=7 and a=7 as well

+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)

+2

Thank you very much Brain & Bones Abhay Aleksei Radchenkov