+ 4
can anyone explain this? var i=3 j=(i++)+(--i)+(++i)+(i++) the answer comes out 14. how?
7 odpowiedzi
+ 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--.
+ 2
@Rockie: I've checked, it's the same.
@Jerome: That interpretation is erroneous. You really need to distinguish between pre and post incrementation.
+ 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
0
If there are no round brackets, what's the answer? The same?
0
hello
0
J=i++ + i++ + ++i + i++ . When i=5.
Find value of i&j?
- 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