int i =2; System.out.println ((i++)+(++i)); //why output is 6..i think output 7; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

int i =2; System.out.println ((i++)+(++i)); //why output is 6..i think output 7;

Java

12th Aug 2020, 10:49 PM
Mohammed Ba tis
Mohammed Ba tis - avatar
3 Answers
+ 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
13th Aug 2020, 2:04 AM
Raina Dhankhar
Raina Dhankhar - avatar
+ 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
14th Aug 2020, 6:33 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
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
12th Aug 2020, 11:14 PM
Edward Finkelstein
Edward Finkelstein - avatar