68.9k views
3 votes
When evaluating ||, what does Java do with the second statement, given that the first one is true?

(e.g., 2 < 1 || x > y

User Khajvah
by
7.9k points

1 Answer

2 votes

Final answer:

Java evaluates the second statement only if the first one is false in a logical OR operation

Step-by-step explanation:

When evaluating the logical OR operator (||) in Java, the second statement is only evaluated if the first statement is false. This is because the logical OR operator returns true if at least one of the statements is true.

In the example you provided, if the first statement '2 < 1' is true, then the second statement 'x > y' would not be evaluated.

Here's an example:

int x = 5;
int y = 3;
if (2 < 1 || x > y) {
// Code inside this block will not be executed
}
User Dejan
by
8.8k points

No related questions found