i can't understand why we need to use switch statement instead of nested if statement there is any speed while checking the condition
-
1What search on google have you tried? – Hawk Nov 20 '14 at 02:37
-
if statement is used when we have to check two condition for example if the condition associated to if is satisfied than the statement associated with that is executed. switch:-switch is a multiconditional control statement .in this we check different conditions in this we uses different cases if case is satisfied the statement associated to that case is executed. – nataraj Nov 20 '14 at 02:56
-
possible duplicate of [What is the difference between IF-ELSE and SWITCH?](http://stackoverflow.com/questions/680656/what-is-the-difference-between-if-else-and-switch) – phuclv Nov 20 '14 at 03:48
-
some other duplicates: http://stackoverflow.com/questions/6211725/nested-if-or-switch-case-statement http://stackoverflow.com/questions/427760/when-to-use-if-else-if-else-over-switch-statments-and-vice-versa http://stackoverflow.com/questions/97987/advantage-of-switch-over-if-else-statement?lq=1 – phuclv Nov 20 '14 at 03:49
3 Answers
The switch statement can be easier to read, which is important for maintenance reasons. Also the compiler might be able to do better optimisations with a switch statement, though compilers are getting pretty good these days, so maybe not.
Looking the other way, multiple if statements (nested or just cascading else-if) can be used in situations where switch/case can not. Eg. comparing strings.

- 117
- 3
As John said, a switch has a specified use. It is limited in what it can do but within those limitations it is usually easier to understand and is less prone to programmer error since the programmer is limited to the capabilities of the switch. So a switch is usually preferred when it can satisfy the requirements. Note that a switch is limited to the one item to be compared to.
Machine efficiency is not likely to be much of a consideration, but developer efficiency can be significant. When developing software to be maintained by others, the owner of the software is likely to require use of conventions that contribute to over-all developer efficiency.

- 2,594
- 3
- 21
- 32
If you take a careful look at the documentation behind switch
control flow, you will see that each case you are switching on ends with a break
. This means that the code block stops executing when the condition of that switch-case has been met.
if
statements are always checked before exiting the block of code, so yes, speed is a big reason behind it. Nothing I have mentioned above is specific to C, or any language, for that matter.
switch(n) {
case 0:
printf("You typed zero.\n");
break;
case 1:
case 9:
printf("n is a perfect square\n");
break;
case 2:
printf("n is an even number\n");
break;
case 3:
case 5:
case 7:
printf("n is a prime number\n");
break;
case 4:
printf("n is a perfect square\n");
case 6:
case 8:
printf("n is an even number\n");
break;
default:
printf("Only single-digit numbers are allowed\n");
break;
}
The code above states that for an argument 'n' , the block will print "You typed zero" if n is 0 (and then stop), will print "n is a perfect square" if n is either 1 or 9 (and then stop), and so on. This is different than its if
control flow counterpart because in the case of if
, the argument would be checked against each condition before the block of code is exited.
You can accomplish similar functionality with if
statements by inserting a 'break' after each if
. switch
control flow is generally appealed to when you are looking to match the argument to a single condition and then exit, as opposed to checking the argument against every single condition.

- 1,399
- 4
- 20
- 34
-
It's `break`, not `BREAK`. If I didn't already understand `if` and `switch` statements, your answer wouldn't tell me which one is supposed to be faster. – Keith Thompson Nov 20 '14 at 02:49
-
`break` is not intended to be used in every `switch` case. It is a `hack`. A `switch` was intended to allow alteration of control **before** the `default` case was applied. The `default` was intended to be part of every case. The `break` in a case is a hack to avoid applying the default. – David C. Rankin Nov 20 '14 at 03:06