Question:
switch statement is closer to which of them..multiple if statements or nested if else statement??
student of honorZ
2006-12-19 22:02:53 UTC
wud anybodylike to tell me....that whether switch statement is more closer to multiple if statements??or switch is more closer to nested if or nested if else statements????i am badly disturbed!!!do reply me as early as possible!!!ill be very thankful to u..:)
Five answers:
heb3
2006-12-20 23:53:18 UTC
Speaking from the point of view of how the compiler *implements* a switch is probably the best way to go.



Very often you have something like:



switch(val) {

case 0: stuff(); break;

case 1: stuff1(); break;

case 2: ...

...

case 29: oddball stuff(); break;

case 87: moreoddballstuff(); break;

default: dodefault(); break;

}



There are often a large number of cases which are regular, that is, separated in value by a fixed amount (not necessarily listed together) along with some oddball cases and perhaps a default.



When possible, those cases that are regularly spaced are implemented by computed branching. If you are switching on a value that is in such a range, the switch() will run very quickly and take little memory to implement as computed branching is very efficient.



What remains is a series of if/else if/else if/else computations or possibly a series of if/if/if statements. Depending on how the compiler ordered these (by value or by order of appearance), your switch could take quite a bit of time to execute (to find the correct "if" statement).



If there are not a series of regularly spaced case values, then it all comes down to a series of tests of either if/else if/.. or if/if/...



It doesn't matter which because in the special situation of a switch statement, the compiler or possibly the optimizer, will modify the generated code so that they wind up the same.



All this gets thrown out if you have fall-through cases, of course.



Some compilers will start out with a min-max range test to see if the value is mentioned in ANY of the cases and either go to the default or do nothing. This could happen even if there are "holes" in the cases as it is typically a very small and fast check to make.



So, there isn't a good one-to-one relationship with the general switch() statement with a particular pattern of if/else if/else or if/if/if statements. It depends on the case values and the compiler implementation and what impact the optimizer has. A lot of work is done by the compiler to make a switch() faster than a pure if/else if/else if/else sequence but if the cases are irregular and widely spaced, it is limited in the optimization.
anonymous
2006-12-19 22:09:57 UTC
Firstly, let me show you an example of the difference:



switch( PlayerState )

{

case 0:

{

// Do state 0 stuff

break;

}

case 1:

{

// Do state 1 stuff

break;

}

default:

{

//Do stuff while in all other states

}

}



if( PlayerState == 0 )

{

//stuff

}

else if ( PlayerState == 1 )

{

//stuff

}

else

{

//other stuff

}



As you can see, both of these chunks of code can do the exact same thing.



The difference between the two really comes in two areas. First off is the asthetics of the code. If you have 100 different states for something, making a switch statement is way better than a huge else/if block.



Secondly is internally how the machine code works. In a switch statement each case is put into a jump table, so that every possible value in the case will only take one jump to get to, rather than in a big if/then statement you have to compute a whole bunch of stuff and jump around a lot before you actually get to the code that needs to be executed.



If you're going to have 3 or more cases, use a switch, if you have less or if you need the more complicated logic of an if/then, then use that.
anonymous
2006-12-19 22:08:59 UTC
A switch statement is more closely related to a nested if.. else if statement. For example:



switch(x) {

case (1): doSomething(1); break;

case (2): doSomething(2); break;

case (3): doSomething(3); break;

default: doNothing(); break;

}



if (x == 1) then {

doSomething(1);

} elseif (x == 2) then {

doSomething(2);

} elseif (x == 3) then {

doSomething(3);

} else {

doNothing();

}



They both do the same, functionally. The difference is, with if statements you can use more conditions. For example, you could do x > 2 or x <= 5. You cannot use conditions like that in a switch statement.
mcgahee
2016-11-28 02:54:50 UTC
If..else and turn statements are rather time-honored in all programming languages. using both is basically own style. some human beings believe that swap statements make code more beneficial readable. Others believe the different. yet what i am going to allow you to recognize is that if..else statements in many cases run somewhat quicker in maximum languages. more beneficial than some thing, it promises more beneficial than one thanks to do some thing, it is sturdy in programming.
anonymous
2006-12-19 22:12:03 UTC
It is like



if(...)

{

body

}



else if(...)

{

body

}



else if(....)

{

body

}



.

.

.

.

.



else

{

body

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...