Differences between IF and SWITCH/CASE in C

11,585

Solution 1

Breifly (your question is vague), a switch compiles to a jump table in assembler and is therefore faster than if / then / else. Note that a switch statement in C has a 'follow-through' feature (google this) which can be circumvented with break statements.

You can only switch on things that evaluate to integral types. In particular this means that you cannot switch on strings: strings are not part of the natural C language in any case.

An if / then / else checks several conditions in succession. Comparison is not restricted to integral types as all you're testing is true (not zero) or false (zero).

That's probably enough to get you started.

Solution 2

When the value to be compared has a type that is amenable to being switch'd, and it makes your code more readable, then go ahead and use a switch. For example,

if (val == 0) {
    // do something
} else if (val == 1) {
    // do something else
} else if (val == 2) {
    // yet another option
} ...

is cluttered and hard to maintain compared to a switch. Imagine that some day, you don't want to switch on val but on validate(val); then you'd need to change all the conditions.

Also, switch may be faster than if/else sometimes, because a compiler may turn it into either a jump table or a binary search. Then again, a compiler might do the same to a series of if/else statements, although that's a more difficult optimization to make because the clauses order might matter and the compiler must be able to detect that it doesn't.

Solution 3

I think If then else is better in case only when you have 2 conditions only. Otherwise its better to use switch case if conditions are more than 2

Solution 4

switch looks better than lots of ifs. However it only works on numeric expressions (as a char is essentially a number, it can still be applied to it, however you cannot use it with strings).

If I may point you to here as it has a nice description of the switch statement. Note the opening sentence:

Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char).

Solution 5

switch is better performance-wise too because it can be optimized in various ways by the compiler, depending on whether the values are consecutive. If yes, it can outright use the value as an index to an array of pointers. If not, it can sometimes use a binary search instead of a linear search, when it's faster.

Share:
11,585
Arrigo Pierotti
Author by

Arrigo Pierotti

Updated on June 04, 2022

Comments

  • Arrigo Pierotti
    Arrigo Pierotti almost 2 years

    The question is really simple: in a laboratory class I attended this year, professor presented the switch/case statement alongside the classic if/then/else statement without saying anything about which one was better in different programming situations.

    Which one is better when checking a variable which can have at least 10/15 possible values?