Does either pose any particular benefit and is there a major difference between them?
Switch Case or If Else If
Collapse
X
-
-
switch is problematic when used badly in terms of maintainability , and can cause inadvertent errors:
In the previous example, the programmer may have not intended to cause case 2 to be executed in both cases. i.e. case 1 will 'do stuff' and 'do other stuff' before leaving the switch. Or the programmer may have done it intentionally. It is hard to say. Commenting is best in this situation if it was intentional.Code:switch(i) { case 1: // do stuff case 2: // do other stuff break; }
However, switch can be very fast and be maintainable if used appropriately. Depending on the compiler and the sparseness of the cases, it may be an O(1) operation to determine which case is to be executed, where an if would be an O(n) operation where n is the number of if/else if's there are.
When you use one over the other is entirely up to you, but you should balance maintainability and speed as needed when you make your decision.
AdrianComment
-
You are partially right. Only integral values are allowed. This includes all signed and unsigned of char, int, long, and long long.Originally posted by pravin78Hi,
I think in switch case only char & int values are allowed but in if else you can also compare real numbers.
Regards,
Pravin
AdrianComment
-
A switch clause is just an integer or anything that can be widened to an int
(such as char, short). The GNU compiler (as an example) recognized three
different ways to compile a switch statement, depending on the case values:
1) perfect
2) dense
3) normal
A perfect switch statement has consecutive case values (in no particular order):Code generated for this switch statement involves a simple jump table:Code:switch (value) { case 3: do3(); break; case 5: do5(); break; case 4: do4(); break; }
0: address of do3();
1: address of do4();
2: address of do5();
the address to be jumped to is table[value-3].
A dense switch statement is like a perfect switch table where all the case
clauses are almost consecutive and the resulting jump table would be dense.
The empty entries point to the address where the (optional or empty) default
case selector is located.
A normal switch statement also uses a jump table and a case value table which
again is sorted. The value table is consulted using a binary search for the case
selector value. If the entry is found, the jump table destination is used,
otherwise the default case clause address will be used.
The first to variants are O(1) while the third variant is O(log(n)) where n is the
number of case clauses in the switch statement. The AIX xlc compiler suite
attempts to find a perfect hash function for a normal switch statement which
can result in O(1) behaviour even for normal (sparse) switch statements. This
attempt can fail and code similar to GNU's compiler generated code for the
normal switch statement is generated.
kind regards,
JosComment
-
shubham agrawal
The "if" statement allows multiple conditions while in switch case we use it only when we have ti check for equality.
Another reason beween them is that "if" condition allows floating points whereas switch condition doesn't allows floating points.Comment
-
In a switch statement you only specify the name of the variable once; in an if-else-if cascade you specify the variable for each leg. Thus, the if-else-if cascade offers you more opportunity to misspell the variable.
It is easier for a maintenance programmer to see that each case refers to the same switch variable than it is to see that each leg of an if-else-if cascade refers to the same variable.
Some static analyzer tools (such as Flexelint) will warn you if the switch variable is an enumerated type and you don't have a case statement for every enumeration constant.
It is generally more important for your code to be easy for a human to understand than it is for the code to be optimized for size or speed.Comment
Comment