Operators and Assignments - Logical Operators
Boolean logical operators & | and ^
- when both operands are boolean the result of the bitwise operators & | and ^ is a boolean
- & - true if both operands are true, otherwise false
- ^ - true if both operands are different, otherwise false
- | - false if both operands are false, otherwise, true
true & true = true; // both operands true
true & false = false; // one operand is false
true ^ false = true; // both operands are different
true ^ true = false; // both operands are the same
true | false = true; // one operand is true
false | false = false; // both operands are false
Conditional AND Operator &&
- both operands must be boolean
- result is a boolean
- returns true if both operands are true, otherwise false
- evaluates the right-hand operand only if the left-hand operand is true
true && true = true; // both operands evaluated
false && true = false; // only left-operand evaluated
Conditional OR Operator ||
- both operands must be boolean
- result is a boolean
- returns true if one of the operands is true
- evaluates the right-hand operand only if the left-hand operands is false
false || true = true; // both operands evaluated
false || false = false;
true || false = true; // only lef-operand evaluated
true || true = true;
The conditional operators are also referred to as short-circuit operators.
Also see
Example Code
|