2
\$\begingroup\$

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.

\$\endgroup\$
12
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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 :) \$\endgroup\$ – DMGregory May 20 '17 at 13:57
  • \$\begingroup\$ You're right, I haven't thought about this while writing the post! I made an edit and included your idea :) \$\endgroup\$ – lyinch May 20 '17 at 14:02
  • 1
    \$\begingroup\$ Multiple short circuits used to be the norm when sending data ... if(con != null && con.open && con.write(data) == successCode) {/*data sent*/} \$\endgroup\$ – Basic May 20 '17 at 22:45
6
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – T. C. May 20 '17 at 17:42
  • \$\begingroup\$ @T.C. Maybe it's some deep wisdom that we are yet to discover. \$\endgroup\$ – S. Tarık Çetin May 20 '17 at 17:44
  • \$\begingroup\$ @S.TarıkÇetin Or maybe... \$\endgroup\$ – T. C. May 20 '17 at 17:45
  • 3
    \$\begingroup\$ -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. \$\endgroup\$ – Jack Aidley May 20 '17 at 18:39
-3
\$\begingroup\$

Short answer, No they won't be checked

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – wizzwizz4 May 20 '17 at 16:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.