C Programming Tutorial 54 – Short Circuit Evaluation

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!

Leave a Reply

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