472,371 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

Switch() vs if else if

Whats the difference b/w swicth case and if else if.
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??

Regards

Jul 23 '05 #1
4 6829
Gurikar wrote:
Whats the difference b/w swicth case and if else if.
The former can only used with integral types, and the case values need to be
compile-time constants. An if/else cascade is likely to be slower than
switch/case.
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??


Heh, I wrote the above before I read this. Well, the reason is that
switch/case is often implemented using a jump table with the case values as
index into the table. The if/else is usually implemented using a cascade of
conditional jumps.

Jul 23 '05 #2

"Gurikar" <ms*******@gmail.com> wrote in message
Whats the difference b/w swicth case and if else if.
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??


Well, you could read about jump table optimization. But measure and then
only believe what you read.

Sharad

Jul 23 '05 #3
Gurikar wrote:
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.


Regards

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
Jul 23 '05 #4

Gurikar wrote:
Whats the difference b/w swicth case and if else if.
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??

Regards


Besides the obvious jumptables, compilers can also generate nested
if-else constructs instead of lineair if-elseif chains. In fact,
these can be mixed.

Asumme you have cases 1..10 and 101..110. Now, a jumptable might be
inconvenient, but implementing such a switch internally as a single
if(x<10) __goto jump[x] else if (x>100&&x<110) __goto jump[x-90]
is certainly legal. Let the compiler deal with those details.

If you would write such code, you'd have to review it every time you
add an enumerator. So does a compiler, but it's a lot faster and
makes less mistakes. Besides, if you did that your source becomes
unreadable. If the compiler does this to your switch, only the
assembly becomes unreadable.

Regards,
Michiel Salters

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Mike and Jo | last post by:
I've been converting some code to C++. I'm trying to use the Switch function to compare a result. Is it possible to use switch to evaluate '>0', '<0', 0? Example switch (result) { case...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
11
by: hasadh | last post by:
Hi, is the assemly code for if..else and switch statements similar. I would like to know if switch also uses value comparison for each case internally or does it jump to the case directly at...
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
13
by: Michael Griebe | last post by:
Simple question. I am optimizing some C++ code and I'd like to know which is faster (or if there is any difference at all) between using a switch statement or nested else-ifs. I'm partial to...
10
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a...
5
by: sam_cit | last post by:
Hi Everyone, I read somewhere that there are some compile time operations behind switch-case, which is why it can work for cases which evaluates to an integer or character and not strings and...
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.