# Are all conditions in an if statement checked? [closed]

If I have an if statement like this:

bool conditionOne = true;
bool conditionTwo = false;
bool conditionThree = false;

if(conditionOne || conditionTwo || conditionThree)
{
//do something
}


Will conditionTwo and conditionThree actually be checked even though conditionOne is already true, and thus has met the requirements of the if statement to run the code inside it? It would be pointless to check if conditionTwo and conditionThree are true because either way the code inside the if statement would be run.

I'm thinking this would be useful to know if I have some really long and quite expensive checks to do.

You are using the logical && and || operators which short-circuit, which means that the logical AND terminates as soon as one false value is found, and the logical OR branches as soon as one true statement has been found. If you want to evaluate the whole condition you can use bitwise operators, like this:

if(conditionOne | conditionTwo | conditionThree){}

This is useful if you call functions in your if-statement which operate on global variables and they should all execute before the evaluation of the if statement. However, this is not a good coding practice...

(edit, as suggested by DMGregory:)

An advantage of short circuit operators is that you don't have to worry about null references, because the condition is simply false if the object has a null reference and therefore skips the rest of the evaluation : if ( myObject != null && myObject.foo > bar ).

https://stackoverflow.com/questions/4820610/is-relying-on-short-circuiting-safe-in-net

• One of the best features of the short-circuited logical operators has to be the ability to write something like this: if ( myObject != null && myObject.foo > bar ), so that if myObject is in fact null, the execution will skip over the second check instead of trying it and causing a NullReferenceException :) – DMGregory May 20 '17 at 13:57
• You're right, I haven't thought about this while writing the post! I made an edit and included your idea :) – lyinch May 20 '17 at 14:02
• Multiple short circuits used to be the norm when sending data ... if(con != null && con.open && con.write(data) == successCode) {/*data sent*/} – Basic May 20 '17 at 22:45

conditionTwo and conditionThree won't be checked. C# evaluates it just like you'd read it:

if(firstIsTrue or secondIsTrue) -> go


It would be so inefficient to check both, or even three of them. Think about all these complex apps, which perform hundred thousand operations per second. It would multiply the evaluation time by 3.

If you don't need to know the second and the third variable state to make a decision, a PC doesn't need too. It depends on the programming language, but most of the modern objective and functional ones doesn't need to know if there's something after the first true evaluation in this kind of condition.

Read this on StackOverflow and remember to ask these kind of questions there, not on GameDev. https://stackoverflow.com/questions/20680943/and-or-logic-for-multiple-condition-statements

Or don't, you'll get destroyed in 30 seconds for this kind of question! :) These are basics, you should learn them by googling.

• If the computer is the most logical thing in the universe, why does it insist that 0.1 + 0.2 = 0.30000000000000004? This question has nothing to do with how logical the computer is. The answer is simply that it depends on the operator being used: in C#, || is a short-circuiting operator, | is not. Bit-fiddling code tends to use the latter. – T. C. May 20 '17 at 17:42
• @T.C. Maybe it's some deep wisdom that we are yet to discover. – S. Tarık Çetin May 20 '17 at 17:44
• @S.TarıkÇetin Or maybe... – T. C. May 20 '17 at 17:45
• -1 Not every language uses lazy evaluation. This is a language specific behaviour, not a property of computers. Moreover the decision to do it this way isn't down to the computer being "the most logical thing in the universe" but because clever language designers implemented it that way. Computers are dumb tools, that they achieve more than that is down to the intellect of their programmers. – Jack Aidley May 20 '17 at 18:39

Short answer, No they won't be checked

• Generally, short answers are discouraged. Why won't they be checked, and when will / won't they be checked? Although, I don't understand the downvotes; this answers the question as asked. – wizzwizz4 May 20 '17 at 16:10