#88 – Conditional Operators

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.

 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.

 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.:

 bool goForWalk = niceWeather && lightOutside;
 bool callPolice = seeBurglar || brokeALimb;
Advertisements