Gurikar said:
Whats the difference b/w swicth case and if else if.
How about a jump table, too?
The switch statement only handles integral quantities.
Compilers may optimize the switch statement into a jump table
(see below).
An if-else-if ladder can handle any type, such as a string.
This construct is more difficult for a compiler to optimize.
A jump table is either a table of addresses (pointers) or
jump instructions. An index is used to access the appropriate
location, then an action is taken. This can be implemented
in C++ using an std::map of <key, function_pointer> or an
array of similar structures.
Iam finding both are same when you are using in application. Only
difference if else if more flexible to use with all data types. But
some people says switch case is faster than if else if, i dont know is
it and why is it??
Only believe "faster than" when actual profiling has been
performed. And only worry about "faster" when the program
is too slow.
The best construct to use is the one that is the easiest
to understand to the reader.
I prefer to use jump tables, because the data can change
without having to retest the jump-table driver (engine).
All these constructs depend on the situation.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library