• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Caleb Curry

Making Money and Investing

  • Home
  • Make Money Online
  • How to Buy Bitcoin
  • Get Started with Real Estate Investing
  • How to Start a Website
  • Courses
    • Full Time Income Online
  • Sponsorship

C Programming Tutorial 54 – Short Circuit Evaluation

November 19, 2018 by Caleb Curry Leave a Comment

In the last blog we went over Complex Conditionals! Check it out first! Are you new here? Start at the beginning, Intro to C!

Short circuiting is something that will happen when your program is running and it hits a conditional.  A short circuit in logic is when you know for sure that an entire complex conditional is either true or false before you evaluate the whole thing.  

You honestly do not have to worry a whole lot about short circuiting when coding because this is something that happens when the software is running, not when you are coding.  That being said, it is still an important concept to understand when it comes to logic.  There is one specific time when this info is very useful.   

We will be discussing short circuiting with the two binary logical operators, AND (&&) and OR (||). 

This really has to do with the unique output for each of the operators.  For the AND operator, there is only one situation when the output is true.  That is when both the left and the right operands are both true. 

If the conditional is being evaluated and we find that the first operand is false, then we know the entire thing is going to be false no matter what.  So why waste our time evaluating the operand?  

For OR, There is only one situation when the output is false.  That is when both the left and the right operands are both false. 

If the conditional is being evaluated and we find that the first operand is true, then we know the entire thing is going to be true no matter what.  So why waste our time evaluating the rest? 

This brings up some shortcuts in logic that are useful to know if you are ever have to do more extensive logic.  

Regardless of what X is: 

False && x = false 

True && x = x 

true || x = true 

False || x = x 

If you just know one, you can negate the whole thing by flipping any false to true or true to false and changing the operator from AND to OR or from OR to AND.

If this all happens behind the scenes, why do we really care as beginners?  The reason is because there are actually two other logical operators I would like to share with you.  | and &, which work exactly the same as || and &&, but they do not do short circuiting.   

When would you do this?  the things you were evaluating were functions and you wanted both to run.  For example:

If we use the non-short circuiting operators, then both of these functions will be ran no matter what.

Let’s read the next blog!

Filed Under: Uncategorized Tagged With: c programming, short circuit evaluation, tutorial 54

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

my face to show my face

My name is Caleb Curry and I started my online business teaching software development and doing digital marketing for brands. Entrepreneurship has changed my life and has allowed me to earn a full time living from anywhere. I now have the freedom and energy to pursue skills and opportunities that I find interesting. This website is all about my journey.

Get my top Python Tips and Tricks

50+ Python Tips and Tricks!

Subscribe to our newsletter to get notes and latest content by email.

    We won't send you spam. Unsubscribe at any time.

    Follow Me!

    • Instagram
    • YouTube

    Recent Posts

    Copyright © 2021 · Genesis Sample on Genesis Framework · WordPress · Log in