IF vs else if

I’ve read more than once to NEVER use else if. I even found a response on Unity Answers that said to never use them. The reason I’m writing this question is to know the difference between the two. I’ve experimented with just using if statements and it always works, because the logic always makes sense. It wasn’t until just a few minutes ago that I discovered a situation where else if is the only way to get some code to work:

when I hit the space key "a" becomes true

if(a && Input.GetButtonDown("left"))
	{
		a = false;
		d = true;
	}
	if(a && Input.GetButtonDown("right"))
	{
		a = false;
		b = true;
	}
	
	if(b && Input.GetButtonDown("left"))
	{
		b = false;
		a = true;
	}
	if(b && Input.GetButtonDown("right"))
	{
		b = false;
		c = true;
	}
	
	if(c && Input.GetButtonDown("left"))
	{
		c = false;
		b = true;
	}
	if(c && Input.GetButtonDown("right"))
	{
		c = false;
		d = true;
	}
	
	if(d && Input.GetButtonDown("left"))
	{
		d = false;
		c = true;
	}
	if(d && Input.GetButtonDown("right"))
	{
		d = false;
		a = true;
	}

The above code didn’t work. It was driving me crazy because everything was in order. I decided to try else if on all of them and sure enough it fixed it. When I had them all as regular if’s when I hit “right” while “a” was true nothing would happen. If I hit “left” it would jump to “c” and completely ignore d. No matter what I tried I could never get “d” to = true. In fact hitting “right” would always make “a” true, doesn’t matter which was currently true hitting right made everything else false and “a” true.

Using else if fixed everything. Now I can cycle through a-d. Imagine something like a= start game, b = load, c= options, d= quit, and that they are all next to each other.
This code cycles through them like it would in any game. So my question is: What’s the difference between if and else if?

I’ve read more than once to NEVER use else if.

What.

I mean, it’s hard to not just kneejerk at a suggestion like that. I would strongly recommend you stop taking programming advice from someone who tells you that sort of stuff. It sounds like they lack any formal training or deep practical experience, and instead rely on cargo cult programming.

As someone who’s been writing code professionally for several years, that’s about the most polite way I can put that. Yeek.

Now, as for your question!

#if… else

  • Exactly one if statement, with a condition
  • Zero or one else statements (with no condition)

If the condition for the if statement is true, the code it contains will be executed.

If the condition is not true, the code inside the else block (if any) will be executed.

#if… else if… else

  • Exactly one if statement, with a condition
  • Zero or more else if statements, each with its own condition
  • Zero or one else statements (with no condition)

If the condition for the if statement is true, the code it contains will be executed.

Otherwise, each else if statement will be checked, in order; if any check passes, the code it contains will be executed, and the rest will be skipped.

Finally, if none of those conditions passed, the code inside the else block (if any) will be executed.

#Only one block per set will execute

With any if... else or if... else if... else chains, at most one of those blocks will execute. If you don’t include a final else, it’s possible that none of them will.

Consider the difference between the following instructions:

  • If there’s a cat, say “meow”. If there’s a dog, say “woof”.
  • If there’s a cat, say “meow”; otherwise, if there’s a dog, say “woof”.

In the first case, a person might say both “meow” and “woof”.

In the second case, a person can only say one or the other.

Finally, it’s also worth noting that all of these can be nested. You can have an if check inside of another if, or an else if, or an else; if you do, that inner check will only be checked if we already passed the outer one.

I’ve figured it out.This has been haunting me for months but I just figured it out. I’ve looked on unity answers multiple times but have never found anything that truly explained it. Else if will only happen if the if statement was false. Because they are in order when I pressed LEFT, A would = false and D = true. The code would continue getting read and when it got to D it would still get the LEFT input from when A = true. OBviously pressing LEFT while D = true will make C = true. That why I would jump to C when I pressed LEFT on A. This also explains why pressing RIGHT always ended with A being true. I thought because it was GetButtonDown it would only read the input once. I guess because it’s in the same frame it counts it multiple times. So I learned 2 things from this. Sorry if it’s a novice question, but I’m kinda teaching this stuff to myself. It’s actually probably a really helpful question that will get asked in the future and this would be a perfect example to teach the difference between the two.