+ 3
How the output is true?
Please explain me🙏 https://code.sololearn.com/cobe8wW3LGvo/?ref=app
14 Réponses
+ 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
+ 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
+ 6
Manash Saikia
As x is pre-incremented, it's value becomes 2. Now, x remains 2.
So, 2==2 is true.
+ 3
Because ++x, increment the value of x by 1. And the present value of x(which was 1) is not retained
+ 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
+ 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;
+ 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
+ 1
Atul Thanks😊
+ 1
Remember: x++ and ++x are different. ++x returns the new value of x, x++ the old. Don't forget ;-)
+ 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