When to use If-else if-else over switch statements and vice versa [duplicate]

Why you would want to use a switch block over a series of if statements? switch statements seem to do the same thing but take longer to type.

38.4k 26 26 gold badges 201 201 silver badges 275 275 bronze badges asked Jan 9, 2009 at 11:33 2,633 6 6 gold badges 28 28 silver badges 34 34 bronze badges

10 Answers 10

As with most things you should pick which to use based on the context and what is conceptually the correct way to go. A switch is really saying "pick one of these based on this variables value" but an if statement is just a series of boolean checks.

As an example, if you were doing:

int value = // some value if (value == 1) < doThis(); >else if (value == 2) < doThat(); >else

This would be much better represented as a switch as it then makes it immediately obviously that the choice of action is occurring based on the value of "value" and not some arbitrary test.

Also, if you find yourself writing switches and if-elses and using an OO language you should be considering getting rid of them and using polymorphism to achieve the same result if possible.

Finally, regarding switch taking longer to type, I can't remember who said it but I did once read someone ask "is your typing speed really the thing that affects how quickly you code?" (paraphrased)

answered Jan 9, 2009 at 11:44 21.1k 10 10 gold badges 59 59 silver badges 69 69 bronze badges

Another way of making it more readable (and looks good in a switch) would be to use Enumerations instead of arbitrary int values

Commented Jan 9, 2009 at 11:51

Enumerations are not native to PHP but can be hacked in as seen in this topic: stackoverflow.com/questions/254514/php-and-enums

Commented Oct 22, 2013 at 0:36 +1 for mentioning polymorphism. Commented Nov 29, 2013 at 14:15

Hands down the winning argument: "immediately obvious that the choice of action is occurring based on the value of "value" AND not some arbitrary test."

Commented Apr 22, 2021 at 16:43 “you should be considering getting rid of them and using polymorphism”. Why? Commented May 25, 2023 at 19:51

If you are switching on the value of a single variable then I'd use a switch every time, it's what the construct was made for.

Otherwise, stick with multiple if-else statements.

answered Jan 9, 2009 at 11:37 Garry Shutler Garry Shutler 32.6k 12 12 gold badges 88 88 silver badges 120 120 bronze badges This is really a very simple yet comprehensive answer. Commented Nov 29, 2013 at 14:14

concerning Readability:

I typically prefer if/else constructs over switch statements, especially in languages that allows fall-through cases. What I've found, often, is as the projects age, and multiple developers gets involved, you'll start having trouble with the construction of a switch statement.

If they (the statements) become anything more than simple, many programmers become lazy and instead of reading the entire statement to understand it, they'll simply pop in a case to cover whatever case they're adding into the statement.

I've seen many cases where code repeats in a switch statement because a person's test was already covered, a simple fall-though case would have sufficed, but laziness forced them to add the redundant code at the end instead of trying to understand the switch. I've also seen some nightmarish switch statements with many cases that were poorly constructed, and simply trying to follow all the logic, with many fall-through cases dispersed throughout, and many cases which weren't, becomes difficult . which kind of leads to the first/redundancy problem I talked about.

Theoretically, the same problem could exist with if/else constructs, but in practice this just doesn't seem to happen as often. Maybe (just a guess) programmers are forced to read a bit more carefully because you need to understand the, often, more complex conditions being tested within the if/else construct? If you're writing something simple that you know others are likely to never touch, and you can construct it well, then I guess it's a toss-up. In that case, whatever is more readable and feels best to you is probably the right answer because you're likely to be sustaining that code.

concerning Speed:

Switch statements often perform faster than if-else constructs (but not always). Since the possible values of a switch statement are laid out beforehand, compilers are able to optimize performance by constructing jump tables. Each condition doesn't have to be tested as in an if/else construct (well, until you find the right one, anyway).

However this isn't always the case, though. If you have a simple switch, say, with possible values of 1 to 10, this will be the case. The more values you add requires the jump tables to be larger and the switch becomes less efficient (not than an if/else, but less efficient than the comparatively simple switch statement). Also, if the values are highly variant ( i.e. instead of 1 to 10, you have 10 possible values of, say, 1, 1000, 10000, 100000, and so on to 100000000000), the switch is less efficient than in the simpler case.