The conditional operators (&&, ||) allow performing a logical AND (&&) or logical OR (||) on its two boolean operands.
The AND (&&) operator returns true only if both of its operands are true.
1
2
3
4
5
| bool b1; b1 = false && false ; // false b1 = false && true ; // false b1 = true && false ; // false b1 = true && true ; // true |
The OR (||) operator returns true if at least one of its operands is true.
1
2
3
4
| b1 = false || false ; // false b1 = false || true ; // true b1 = true || false ; // true b1 = true || true ; // true |
You’d normally use boolean variables in expressions where conditional operators are used, e.g.:
1
2
| bool goForWalk = niceWeather && lightOutside; bool callPolice = seeBurglar || brokeALimb; |