Logical Operators return boolean result which makes them suitable for if statements.
Operator | Description | Examples | Result |
---|---|---|---|
! | Not - inverse the value. | !true | false |
!false | true | ||
!(2 + 3 == 5) | false | ||
&& | And
|
true && true && true | true |
false && false && false | false | ||
true && false | false | ||
|| | Or
|
true || true || true | true |
false || false || false | false | ||
true || false | true |
Let's look at the following example. Depending on a test score, determine whether the score is low, medium, high or invalid following these rules:
Output:
Explanation:
Line 4
checks whether number is greater or equal to 0
and if it's less or equal to 50.Line 16
checks whether number is less than 0 or if it's greater than 100.