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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: