Comparisons with the Logical "And" Operator
Sometimes you will need to test more than one thing at a time. The logical and operator (&&) returns true if and only if the operands to the left and right of it are true.
The same effect could be achieved by nesting an if statement inside another if:
if (num > 5) {
if (num < 10) {
return "Yes";
}
}
return "No";
The above code will only return Yes if the num is greater than 5 and less than 10. The same logic can be written as:
if (num > 5 && num < 10) {
return "Yes";
}
return "No";
Comparisons with the Logical "Or" Operator
The logical or operator (||) returns true if either of the operands is true. Otherwise, it returns false.
The logical or operator is composed of two pipe symbols: (||). This can typically be found between your Backspace and Enter keys.
The pattern below should look familiar from prior waypoints:
if (num > 10) {
return "No";
}
if (num < 5) {
return "No";
}
return "Yes";
The above code will return Yes only if the num is between 5 and 10 (5 and 10 included). The same logic can be written as:
if (num > 10 || num < 5) {
return "No";
}
return "Yes";
Post a Comment