Else and If Statements

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?


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.

1 Like

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.

Okay thanks, that clears up the confusion

'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

public int number = 10;

if(number > 15)
{
    //execute this code only if the number is greater than 15
}
else if(number < 7)
{
    // 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
}
else
{
    //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
}

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.

public int number = 10;

if(number > 15 || number < 7)
{
   // execute this code only if number is greater than 15 OR less than 7 so this code will not execute
}

if(number > 15 && number < 7)
{
    // execute this code only if number is greater than 15 AND number is less than 7 so this code will not execute
}

if(number == 10 && number < 15)
{
    // execute this code only if number equals 10 AND number is less than 15 so this code WILL execute
}

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

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:

// decides which to do - always does exactly one:
if(...) thing1
else thing2

// could do both, either, or none:
if(...) thing3
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:

// always picks one thing to do:
if(...) thingA
else if(...) thingB
else if(...) thingC
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):

// always does 2 things:
if(...) thing1
else if(...) thing2
else thing3
if(...) thingA
else thingB