Control Statements
Logical operators
Operator | Name | Description |
---|---|---|
&& | And | Returns a true value if both expressions are true. This operator only evaluates the second expression if necessary. |
|| | Or | Returns a true value if either expression is true. This operator only evaluates the second expression if necessary. |
& | And | Returns a true value if both expressions are true. This operator always evaluates both expressions. |
| | Or | Returns a true value if either expression is true. This operator always evaluates both expressions. |
! | Not | Reverses the value of the expression. |
Examples of logical operations
subtotal >= 250 && subtotal < 500
timeInService <=4 || timeInService >= 12
isValid == true & counter++ < years
isValid == true | counter++ < years
(subtotal >= 250 && subtotal < 500) || isValid == true
!(counter++ >= years)
How to use the logical operators
- You can use the logical operators to create a Boolean expression that combines two or more Boolean expressions.
- By default, Not operations are performed first, followed by And operations, and then Or operations.
- Since the && and || operators evaluate the second expression only if necessary, they can be referred to as short-circuit operators.