Search Unity

Question Else and If Statements

Discussion in 'Scripting' started by junior333istall, Apr 1, 2023.

  1. junior333istall

    junior333istall

    Joined:
    Jan 11, 2023
    Posts:
    101
    Should you always add an else after an if statement. And should you add an else after you use an else if statement. And when should you use an esle if? When to use a seperate if?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Only if you need something to run when the if condition was not true.
    Only if you need something to run when all of the if and else/if conditions were not true.
    When you want to run something only when the if condition was NOT true, but also need some additional condition to be true.
    When it's unrelated to any other if conditions.
     
    Bunny83 likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,934
    You should only use an else, of an if-else... when you need it. That's it really. There's no 'should' here.

    It just comes down to what logic you require.
     
  4. junior333istall

    junior333istall

    Joined:
    Jan 11, 2023
    Posts:
    101
    Okay thanks, that clears up the confusion
     
  5. casezack177

    casezack177

    Joined:
    Aug 10, 2021
    Posts:
    6
    'If' statements can be used by themselves if needed. They are simply conditions to be checked before running a certain piece of code. You don't explicitly need an 'else' statement or an 'else if' statement.

    'else if' statements will continue on an 'if' chain. When the conditions for the first 'if' statement are not met then the 'else if' condition will be checked. You can use any number of else if statements to continue checking additional conditions.

    An else statement will only go at the end of your if chain and will function as a catch if none of the above conditions are met.

    check the sample code below

    Code (CSharp):
    1. public int number = 10;
    2.  
    3. if(number > 15)
    4. {
    5.     //execute this code only if the number is greater than 15
    6. }
    7. else if(number < 7)
    8. {
    9.     // execute this code only if number is less than 7 AND greater than 15 since it has to pass through the first if statement before reaching this one
    10. }
    11. else
    12. {
    13.     //do this only if number is not less than 7 and not greater than 18. meaning this loop will come all the way down to the else statement since the number 10 is less than 15 and greater than 7
    14. }
    When using 'else if' statements you should only have 1 'if' statement at the start followed by as many 'else if' statements as you have conditions to check and then if you have a piece of code that you would like to run when NONE of the 'if' or 'else if' conditions are met you should end the chain with an 'else' statement.

    A very important thing to note about 'If' 'else if' and 'else' is that only one condition will ever be met in the chain as soon as one of the conditions is met no matter where it is the chain will exit


    You can also check multiple conditions in a single 'if' statement by using the AND/OR operators.

    Code (CSharp):
    1. public int number = 10;
    2.  
    3. if(number > 15 || number < 7)
    4. {
    5.    // execute this code only if number is greater than 15 OR less than 7 so this code will not execute
    6. }
    7.  
    8. if(number > 15 && number < 7)
    9. {
    10.     // execute this code only if number is greater than 15 AND number is less than 7 so this code will not execute
    11. }
    12.  
    13. if(number == 10 && number < 15)
    14. {
    15.     // execute this code only if number equals 10 AND number is less than 15 so this code WILL execute
    16. }
    With the above code do note that each 'if' statement is individual so each 'if' statement will be check regardless of whether the ones before it are executed or not.

    I hope this helps, you can read more about this on plenty of different coding sites. This is not unique to unity or c# it is a basic coding principle. Please check out the links below.

    https://www.w3schools.com/cs/cs_conditions.php
    https://www.tutorialspoint.com/csharp/if_else_statement_in_csharp.htm
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    There are some things about IF's that aren't new rules, but take a while to get used to: an if/else always does exactly one thing, where-as 2 If's in a row can do both, none or either:
    Code (CSharp):
    1. // decides which to do - always does exactly one:
    2. if(...) thing1
    3. else thing2
    4.  
    5. // could do both, either, or none:
    6. if(...) thing3
    7. if(...) thing4
    A cascading multipart IF isn't a new rule -- it's just lots of if/else's together, but if feels like a special thing. If it has a final else it always does exactly one thing. It's like a multi-way if:
    Code (CSharp):
    1. // always picks one thing to do:
    2. if(...) thingA
    3. else if(...) thingB
    4. else if(...) thingC
    5. else thingD
    But without the final else if might do one thing in the list, or might do nothing.

    If you have several if-piles in a row it just adds up. For example, this always does two things (if's a 3-way if/else followed by a 2-way one):
    Code (CSharp):
    1. // always does 2 things:
    2. if(...) thing1
    3. else if(...) thing2
    4. else thing3
    5. if(...) thingA
    6. else thingB