#1,201 – Writing Conditional Logical Operators as Conditionals

The conditional logical operators (&&, ||) can short-circuit evaluation, based on the value of the left side of the expression.

  • For &&, evaluation short-circuits if left-side is false (result is false)
  • For ||, evaluation short-circuits if left-side is true (result is true)

You can also think of the conditional logical operators as being equivalent to the ternary conditional operator.

            bool leftSide = true;
            bool rightSide = false;

            // Short-circuits (right side not evaluated)
            bool bResult = leftSide || rightSide;

            // || Equivalent to
            bResult = leftSide ? true : rightSide;

            // Short-circuits if left side is false
            bResult = leftSide && rightSide;

            // && Equivalent to
            bResult = leftSide ? rightSide : false;
Advertisement

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

One Response to #1,201 – Writing Conditional Logical Operators as Conditionals

  1. Pingback: Dew Drop – October 10, 2014 (#1874) | Morning Dew

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: