This is because i--
results to give the value before the decrement operation will be performed. However, in --i
i will be get decremented before its value will be used.
For understanding it better:
Lets use your example:
i=2;
Case 1:
while(i+1?--i:14)
First pass:
Condition i+1?--i:14
will be evaluated as 2+1 ? 1 : 14
This will return 1
. The loop will be executed and i
will contain the value 1
.
Second pass:
Condition i+1?--i:14
will be evaluated as 1+1 ? 0 : 14
This will return 0
. Hence loop will not be executed, therefore you are getting output as 1
.
case 2:
while(i+1?i--:14)
First pass:
Condition i+1?i--:14
will be evaluated as 2+1 ? 2-- : 14
This will return 2
. The loop will be executed and i
will contain the value 1
.
Second pass:
Condition i+1?i--:14
will be evaluated as 2+1 ? 1-- : 14
This will return 1
. The loop will be executed and i
will contain the value 0
.
The while
loop will be executed twice in this case.