#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;

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #88 – Conditional Operators

  1. Peter Chamberlin says:

    Typo: “performing and logical AND” → ‘performing a logical AND’

Leave a comment