How does Unity reads the "If" conditions?

Hello guys, I’m trying to optimize my game, and I do a lot of conditions like this in the update function:

if(boolean == true && variable > 5)

Let’s say that the boolean is false.
I assume that unity will not even check the variable, because the boolean is false, am I right?

Or I have to do in separate If’s?

if ( boolean == true )
{
if (variable > 5)
{}
}

this second case, i’m sure that unity won’t check the variable, but most of my code I wrote usign the first exemple to prevent poluting the lines…
I’m asking this, because i saw lots of scripts that people wrote just like the second exemple, and I want to know wich is better, because i want unity to avoid checking unnecesary variables every time.

3 Answers

3

Both C# and Javascript use short-circuit boolean. That means that if the first condition is false, the second will not be executed. Short-circuit boolean is done in a majority of languages, but it’s not true of all programming languages.

Note you don’t have to do ‘boolean == true’ you can just ‘boolean’ So you code would be:

if (boolean && variable > 5)

Note you can test a language by using a function that returns a boolean in an ‘if’ statement:

function BoolTest() : boolean {
    Debug.Log("Testing");
    return false;
}

Then to test:

if (BoolTest() && BoolTest()) {
   //
}

If you only get one ‘Testing’, then it is Short circuit boolean.

Thank You very much! :)

&&” and “||” are shortcut boolean operators, working from left to right the program will only evaluate as much as is strictly necessary.

The Wikipedia page on “short circuit evaluation” will go into further depth.

If you wanted to evaluate the expression completely (e.g. to trigger side effects) then you could use “|” and “&”.

update:

Generally speaking chaining functions with side effects into complex boolean statements is discouraged. Separating functions that query state from methods that change state will usually result in code that is easier to understand and maintain.

However as an example I offer:

bool strengthRoll() {
    if (Random.Range(0,11) < 8 ) {
        strengthXP++;
        return true;
    }
    return false;
}

bool dexterityRoll() {
    if (Random.Range(0,11) < 8 ) {
        dexterityXP++;
        return true;
    }
    return false;
}

//then in the update:
if (buttonPressed()) {
    if (strengthRoll() & dexterityRoll()) {
        //special attack
    }
}

Here each of the stat rolls has the sideeffect of incrementing a separate XP counter.

strengthRoll and dexterityRoll are joined by the “&” so they will both be called even if the first returns true, as a result their XP will increase evenly.

If they were joined by the “&&” then dexterityRoll would only be called when strengthRoll returns true, in that scenario dexterityXP would increase at a lower rate to strengthXP.

As you can see the impacts of operator selection can have subtle effects on program behavior. While it is important to be able to recognize it when reading other peoples code , I would try to favor a more overt style when writing. One of best measures of code quality is that it does what you expect of it.

Thanks for the awser! But in that case, can you give me a exemple in wich its better to use "&" instead of "&&"? I can't think of any!

This also might bu useful: If \ Else how does the program reads it? - Unity Answers