+ 1
int i =2; System.out.println ((i++)+(++i)); //why output is 6..i think output 7;
Java
3 Réponses
+ 8
Increment operator (++): It increments the value by 1. There are two variations of increment operators.
Post-Increment: First use the variable after that increment the value of a variable.
Pre-Increment: It is the opposite of post-increment. In this first increment, the value and after that used the value of a variable.
The evaluation of these operations start from right to left.
Pre increment: ++i, it increment the value and assign to i and now value is 3.
Post increment: i++, it increment the value but doesn't assign to i so value still 3
That's why answer is 6.
For more details:https://javagoal.com/operators-in-java/#2
+ 7
Learn concepts of increment and decrement without know it u cannot calculate .
int a= 5,b;
b= ++a + ++a + a++ + a
its very confusing so without knowing concepts you can not solve
0
i++ assigns i the value of 2, then i increments to 3
++i increments the value of i to 4
then
2+4=6