How the output is true? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
+ 3

# How the output is true?

28th Mar 2021, 3:20 PM
Manash Saikia [ 45% Active ]
11 ответов
+ 17
Manash Saikia Because ++x is pre increment so ++x will be 2 and also x will be 2 So 2 == 2 //true (x++ == x) //returns false because of post increment
28th Mar 2021, 3:27 PM
A͢J
+ 6
public class Program { public static void main(String[] args) { int x=1; int y=x;//stores the value of x System. out. print (++x==y);//now x's value is 2,but y's value is remained unaltered } } //Hope this helps you to understand it better
28th Mar 2021, 3:32 PM
Atul [Inactive]
+ 6
Manash Saikia As x is pre-incremented, it's value becomes 2. Now, x remains 2. So, 2==2 is true.
30th Mar 2021, 4:38 AM
ᗩηιηɗуα ᗩɗнιкαяι
+ 5
🅰🅹 Farnaz.A Thank you😊
28th Mar 2021, 3:30 PM
Manash Saikia [ 45% Active ]
+ 3
Because ++x, increment the value of x by 1. And the present value of x(which was 1) is not retained
28th Mar 2021, 3:30 PM
Atul [Inactive]
+ 3
Pre increment ie (++x) increments x before evaluation while post increment ie (x++) increments x after evaluation.. Therefore it ++x==2 returns false while x++==2 returns true
30th Mar 2021, 4:44 AM
Simon Muthoni
+ 2
x++ = x Because x++ will increment value after evaluation so x++ == x is true On the contrary, ++x will increment value before evaluation Then result false if use ++x(2) == 1 Then, ++x can increment value before evaluation so print additional value while increment value☺ Example: cout << ++x << endl;
29th Mar 2021, 12:55 PM
Isabella
+ 2
In such expressions where there is no priority, the phrase is evaluated from the right. In this way, the value x is first equal to two, and then it is compared to the left, which is also two on the left.(because ++x) is prefix operand first add 1 to it then evaluated
29th Mar 2021, 7:45 PM
Mehran
+ 1
Atul Thanks😊
28th Mar 2021, 3:36 PM
Manash Saikia [ 45% Active ]
+ 1
Remember: x++ and ++x are different. ++x returns the new value of x, x++ the old. Don't forget ;-)
29th Mar 2021, 5:42 PM
Sebastian Ahlborn
+ 1
Because ++x means take your x use it wherever you want after all increase just 1 in your program, java compiler takes x value compares to whatever you wish after all these it increases just 1
29th Mar 2021, 6:16 PM
Bekir Safa Yesil