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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

question about switch

Discussion in 'Editor & General Support' started by PersianKiller, Feb 6, 2018.

  1. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    Code (CSharp):
    1.  
    2.  
    3.     public void Switch(){
    4.  
    5.         var a = 100;
    6.         switch (a){
    7.         case 1:
    8.             Debug.Log ("1");
    9.             break;
    10.  
    11.         case 2:
    12.             Debug.Log ("2");
    13.             break;
    14.  
    15.         }
    16. .
    17. .
    18. .
    19.   case 100:
    20.             Debug.Log ("100");
    21.             break;
    22.  
    23.         }
    24.  
    25.     }
    26.  
    27.  

    my question is that, if I set a to 100 ,it will access to case 100 directly ????
     
    Last edited: Feb 6, 2018
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    PersianKiller likes this.
  3. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    thanks for response,so what's the difference between switch and else if ?
     
  4. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    Different compiled code. Subtle speed differences depending on number of cases. Nothing to be worried about unless it impacts performance. But you would need a lot of cases for that.
     
    PersianKiller likes this.
  5. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    :rolleyes: hmmm thanks a lot :)