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?
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.
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.
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 . 19 . If you have statistics on the expected frequency of condition evaluations , you should use them to make your program faster and more efficient .
No it won't.
||
, if first term is true
second term won't
be evaluated.|
both terms are evaluatedSimilarly...
&&
, if first term is false
second term won't be evaluated&
, both terms are evaluatedJava operators docs here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With