like most everyone said, the else-if will go through each statement and check to see if its true. the switch goes directly to the true statement (if any). The advantage can be seen in HOW they compile. If you have only a few conditions, then it doesnt really matter what method you use, If you have many conditions then the switch is the way to go. when compiled the compiler makes a jump table for all of the cases and when executed will jump directly to the proper case. If you use an else-if, then when compiled and executed, it will go through EVERY else-if and look for a true statement. with lots of else-if's it takes time which means its less efficient.
2006-12-06 01:47:34
·
answer #1
·
answered by justme 7
·
0⤊
0⤋
A switch allows for results to continue, an else-if only allows one result.
For example (not actual c code by any means):
a = 3
b = 0
if (a > 0) {
b = b +1
} else if (a > 1) {
b = b +1
} else if (a >2) {
b = b +1
} else if (a > 3) {
b = b + 1
}
Here b = 1 because it matched the first if and then stopped.
a = 3
b = 0
switch (a) {
case (>0) b = b + 1
case (>1) b = b + 1
case (>2) b = b + 1
case (>3) b = b + 1
}
Here b = 3 (note that 3 is not greater than 3, it is equal to). The switch continues checking the cases even if it matches a case. It keeps going until it hits a break statement.
a = 3
b = 0
switch (a) {
case (>0) b = b + 1
case (>1) b = b + 1; break
case (>2) b = b + 1
case (>3) b = b + 1
}
Here b = 2 because the break statement ends the switch. Note that this is the reason for a switch in almost all programming languages, but some will auto-break if a case matches (therefore having no difference between an else-if and a switch)
2006-12-05 22:51:42
·
answer #2
·
answered by Bryan A 5
·
0⤊
1⤋
else if :-- else if is the control statement which controls the statement(s) to be executed on the basis of some condition. When we use else-if, the condition is checked and if the condition is true corresponding statement(s) is executed. If the condition is false, it continues checking the next else-if statement until the condition becomes true or the control comes to the end of all else-ifs.
switch :--In this case, a statement is written inside switch, and then, the probabale cases are written. Now, when control comes to the switch statement, it compares the value with the cases, if it matches any cases, corresponding statements inside the case is executed n the control checks for next statement, and do the same, until it reaches the end of the switch. To, make the prog execute only one case, v use break inside every case.
Diff :----
In case of else-if, the control checks every else-if statement until it find true value of the statement or it comes to the end of the else-ifs. In case of switch, according to the value of switch, the control jumps to the corresponding case.
When, we want multiple types of statements to be executed when condition becomes true, we should use switch without break in the case. Whereas in case of else-if, we will have to use the common statements in desired else-ifs, which causes duplication of some of the statements.
2006-12-06 02:45:08
·
answer #3
·
answered by kumar pritam 1
·
0⤊
0⤋
An else-if is a more general format for testing multiple values, as compared to a switch statement that can only test for integer values.
However it is this restriction that provides the power to switch case statement, in that the comparison could be internally implemented as a look-up table leading to typically much faster look-ups as compared to a else-if construct.
Also the fall-through behavior of a switch-case construct is something that is not available in a else-if construct.
2006-12-05 22:47:09
·
answer #4
·
answered by swami060 3
·
0⤊
1⤋
the switch is expansion of the else if.when we using the
else if it will check all condition but the switch match
the case and it will break from the switch.the else if
can contain any type of condition but the switch must
contain same data type cases.
2006-12-05 22:47:28
·
answer #5
·
answered by aravind prabakar 1
·
0⤊
0⤋
Suppose that you have 20 options, switch simply makes it easier to manage and update later.
Good luck and Happy Computing!
2006-12-05 22:43:52
·
answer #6
·
answered by Anonymous
·
0⤊
0⤋