Full explanation of "If, else if and else" the way I understand it (only "else")

This is what I do in visual studio:

I know it’s not the same like Rick do it, because in the end I decide to do like the example he give it earlier with if… then else if… and in the end with just else…, but I understand why should be like if else and when can be else only. For example:

//Let say for this example our apples are 12
int apples = 12;

// If apples are 12 is true
if (apples = 12)
{
//then a text will display
Debug.Log(“Your apples are 12.”);
}
//let say we decide to change variable value from 12 to 5
else if (apples = 5)
{
//then the text will say my apples are 5 not 12
Debug.Log(“Your apples are 5.”);
}
//and lastly if my variable value is not 12 or 5
else
{
//then the text will be change to this one
Debug.Log(“Your apples are not 12 and they are not 5 either.”);
}

So in other words, the way I understand it, else (without if) can be used when none of the other if options are true for the statement. But in this case if you write else{if()} or else if, there is not very big difference except the visual that said else if come together and that’s why I think Rick did it that way in the tutorial, but both will work. At least they work for me. Also there is only slightly differents than before. You can again click both buttons in same time and sometimes to get same result as before, but it different with sure. So, check for difference if you start to think, that it’s not working. If it’s same you have a mistake, but if it’s done right don’t expect big changes too.

1 Like

Amazing job describing it and showing an example! This will help you understand the material better.

1 Like

Thanks! That’s the idea! :slight_smile:

1 Like

I love it!

Privacy & Terms