java logical operator || I have a question

Asked 2 years ago, Updated 2 years ago, 24 views

int x = 100;
int y = 200;
boolean result = false;


result = x > y || y++ > 200;
System.out.println("x > y || y++ > 200 = " + result);

|| The operator says that if either of them is true, it displays true If it's y++, isn't the value 201? Then shouldn't it be marked as true?

java

2022-09-21 19:24

1 Answers

I don't know much about Java, but... When I searched java++ operation on Google, this information came out.

The point is, ++a increases the value and then returns it right away. a++ also increases the value (behind), but returns the value that has not yet changed. So it actually seems like it's going to work out later.

Then maybe you want:

int x = 100;
int y = 200;
boolean result = false;
result = (x > y || ++y > 200); // <-- Important here
System.out.println("x > y || ++y > 200 --> " + result); // --> true


2022-09-21 19:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.