Here's a perfect illustration I saw.
&&
if (num > 5 && num < 10) {
return "Yes";
}
return "No";
||
if (num > 10 || num < 5) {
return "No";
}
return "Yes";
What's the difference between these two?
Here's a perfect illustration I saw.
&&
if (num > 5 && num < 10) {
return "Yes";
}
return "No";
||
if (num > 10 || num < 5) {
return "No";
}
return "Yes";
What's the difference between these two?
They are AND (&&) and OR (||) operators.
In your first example the response will only be "Yes" if num
is greater than 5 AND less than 10, so any value for num
between 5 and 10 (exclusively) will return "Yes".
In your second example "No" will be returned if num
is greater than 10 OR less then 5, so any value between 5 and 10 (inclusively) will return "Yes".
To make the expression
num > 5 && num < 10
negative,
!(num > 5 && num < 10)
you need to apply De Morgan's law
!(num > 5) || !(num < 10)
and reverse the condition inside of the parens
num <= 5 || num >= 10
In your question you have the expression with logical Or ||
num > 10 || num < 5
which is, despite the order, obviously not the same as the above expression. The result of comparing values is not the same.