posted 17 years ago
hello,
I saw an example in the K/B book, about logical operators:
int y = 5;
int x = 2;
if ((x > 3) && (y < 2) | doStuff()) { // OR is a Not Short-Circuit operator
System.out.println("true");
}
The code does not print anything, because there is used the "&&" operator (short-circuit operator), that doesn't check the second operand if the first is false. In this example, (x>3) is false, so it doesn't check anymore what is after "&&".
This is ok, I understand, but my dilema is that if I change a little bit the code (the OR operator is no longer Not Short-Circuit, but is a Short-Circuit), the output is "true".
int y = 5;
int x = 2;
if ((x > 3) && (y < 2) || doStuff()) { // OR operand is Short-Circuit
System.out.println("true");
}
Can anyone tell me why the second operator changes the result ?
Thank you