Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does logical or ignores second statement if first is true [duplicate]

In Java (Eclipse), when having a statement such as if (true || false), it will end up true but the question is will the compiler evaluate the second statement if the first is true? This is of an importance to me because I have an operation I need to do if the variable is null OR it has a certain value. My statement looks like if (array == null || array[i] < array[j]). You can see the reason for my question, because if array is set to null then the second statement will produce an error. So, will the true from array == null suffice or will the compiler evaluate array[i] < array[j]) also?

like image 613
Sharon Dorot Avatar asked May 09 '14 10:05

Sharon Dorot


People also ask

Does or check second condition if first is true?

OR Operator. OR operator is also a logical operator and it displays a record if either the first condition or the second condition is TRUE. If all the conditions are FALSE then the SQL statement won't return any result.

Which operator does not check 2nd condition if 1st condition is false?

No while evaluating && if the first condition is false then it doesn't evaluate the second condition. Similarly while evaluating || if the first condition is true then the second condition is not evaluated. Save this answer.

Does or return true if both are true?

An expression using the OR operator will evaluate to TRUE if the left operand or the right operand is TRUE. If both are TRUE, the expression will evaluate to TRUE, however if neither are TRUE, then the expression will be FALSE.

When two conditions joined by the logical and operator are evaluated if the first one is false there is no need for the computer to evaluate the second one true or false?

When two conditions joined by the logical And operator are evaluated , if the first one is false , there is no need for the computer to evaluate the second one . 19 . If you have statistics on the expected frequency of condition evaluations , you should use them to make your program faster and more efficient .


1 Answers

No it won't.

  • With boolean operator ||, if first term is true second term won't be evaluated.
  • With bitwise operator | both terms are evaluated

Similarly...

  • With boolean operator &&, if first term is false second term won't be evaluated
  • With bitwise operator &, both terms are evaluated

Java operators docs here.

like image 200
Mena Avatar answered Oct 05 '22 21:10

Mena