Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

If statement with multiple conditions question

Discussion in 'Scripting' started by Crawdad, Mar 16, 2022.

  1. Crawdad

    Crawdad

    Joined:
    Nov 23, 2016
    Posts:
    172
    Hey guys. Just had a quick question regarding performance of if statements. So if I have an if statement with multiple conditions, are all conditions checked no matter what the outcome, or as soon as one fails does it break and move on? Using the following as an example...

    Code (CSharp):
    1. if (someBool && Physics.Linecast(thisPosition, thatPosition))
    If someBool = false, does it still check the linecast anyways before moving on? Thanks in advance.
     
    kaancetinkayasf likes this.
  2. kaancetinkayasf

    kaancetinkayasf

    Joined:
    Feb 20, 2020
    Posts:
    10
    No it does not, 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.
     
    Crawdad and Bunny83 like this.
  3. Trafulgoth

    Trafulgoth

    Joined:
    Jun 5, 2013
    Posts:
    45
    It depends on what operation you use. For 'and' (&&), it will stop evaluating if the first is false (and so on, if you have more than two). For 'or' (||), it will stop evaluating if the first is true. This is call "short circuiting", in case you want a term to research.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Nope, if statements will resolve left to right. In the case of &&, it stops as soon as it hits a false. In the case of || it does need to check all the or conditions until it hits a true.
     
    Crawdad likes this.
  5. Crawdad

    Crawdad

    Joined:
    Nov 23, 2016
    Posts:
    172
    Ok, thanks guys! I thought it would be silly for it to keep running, but was looking at ways to increase performance on some of my scripts, and thought that was worth looking at. I suppose that means it's worth paying attention to the order in which you place them so your least performant checks are at the end.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    If your game is running slow, it's probably not due to the ordering in your IF statements. You would want to use the Profiler.
     
    Crawdad, Kurt-Dekker, Bunny83 and 2 others like this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    If you go here: https://godbolt.org/ and select C# in the left window and paste this contrived code:
    Code (CSharp):
    1. class Program
    2. {
    3.     static int A = 0;
    4.     static int B = 0;
    5.     static int C = 0;
    6.  
    7.     static bool CheckA()
    8.     {
    9.         return ++A > 50;
    10.     }
    11.  
    12.     static bool CheckB()
    13.     {
    14.         return ++B > 50;
    15.     }
    16.  
    17.     static void Main()
    18.     {
    19.         // We want to see if A and B are calculated.
    20.         if (CheckA() && CheckB())
    21.         {
    22.             C++;
    23.         }
    24.     }
    25. }
    26.  
    ... you should be presented with the following compiler output:
    Output.png

    On line 7 you can see the "CheckA()" call and line 10 you can see the "CheckB()" call. On line 9 you can see (and click) that it jumps over the test for "CheckB()" (actually right over the whole "if" stuff).

    This is technical but it's the also the most direct evidence that this is what happens.
     
    Crawdad and Kurt-Dekker like this.