can anyone explain this? var i=3 j=(i++)+(--i)+(++i)+(i++) the answer comes out 14. how? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

can anyone explain this? var i=3 j=(i++)+(--i)+(++i)+(i++) the answer comes out 14. how?

25th Oct 2016, 7:27 AM
priyam chopra
priyam chopra - avatar
7 Answers
+ 10
Step by step: j = (i++)+(--i)+(++i)+(i++), i = 3 j = 3+(--i)+(++i)+(i++), i = 4 j = 3+3+(++i)+(i++), i = 3 j = 3+3+4+(i++), i = 4 j = 3+3+4+4, i = 5 j = 14, i = 5 Note: ++i increments i, then evaluates it (pre-incrementation). i++ evaluates i, then increments it (post-incrementation). Same deal for --i and i--.
25th Oct 2016, 8:19 AM
Zen
Zen - avatar
+ 2
@Rockie: I've checked, it's the same. @Jerome: That interpretation is erroneous. You really need to distinguish between pre and post incrementation.
25th Oct 2016, 1:46 PM
Zen
Zen - avatar
+ 2
If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing. Syntax Operator: x++ or ++x Examples // Postfix var x = 3; y = x++; // y = 3, x = 4 // Prefix var a = 2; b = ++a; // a = 3, b = 3 Decrement (--) The decrement operator decrements (subtracts one from) its operand and returns a value. If used postfix (for example, x--), then it returns the value before decrementing. If used prefix (for example, --x), then it returns the value after decrementing. Syntax Operator: x-- or --x Examples // Postfix var x = 3; y = x--; // y = 3, x = 2 // Prefix var a = 2; b = --a; // a = 1, b = 1
5th Dec 2016, 1:12 AM
DN Kiri Phangcho
DN Kiri Phangcho - avatar
0
If there are no round brackets, what's the answer? The same?
25th Oct 2016, 1:39 PM
Rockie
Rockie - avatar
0
hello
13th May 2017, 8:09 PM
Vivek Tiwari
Vivek Tiwari - avatar
0
J=i++ + i++ + ++i + i++ . When i=5. Find value of i&j?
8th Dec 2017, 1:43 PM
Himank Shah
Himank Shah - avatar
- 5
Step by step: j = (i++)+(--i)+(++i)+(i++) j = (3+1)+(3-1)+(3+1)+(3+1) j = 4+2+4+4 j= 14
25th Oct 2016, 12:58 PM
Jerome Quijano
Jerome Quijano - avatar