Switch vs. If/Else

Started by
17 comments, last by the_grip 22 years, 2 months ago
I believe that some compilers will optimize larger switches into a search tree (binary, I believe), which on average will be faster that an if/else.
Advertisement
quote:Original post by CaptainJester
A switch/case statement is exactly the same as an if/else set.

Perhaps if you're programing in (Object) Pascal or VB - we like C/C++ here

A switch uses a jump table - one of the requirements of the C switch is that each case be a const integral value to allow this implmentation. If the cases change at run-time (non-const), you need code there to do the check.

NULL is a macro, and that probably has something to do with why the case rejects it. Just use 0.

Switches are preferred to chained-if's. And virtual functions are preferred to switch blocks. There are cases when using the former makes more sense (if speed is paramount, use the switch). If extensiblity and solid architecture is important, use virtuals. If the cases are nebulous but unchanging, use chained if's.

Edited by - Magmai Kai Holmlor on January 30, 2002 9:12:07 PM

Edited by - Magmai Kai Holmlor on January 30, 2002 9:12:23 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Another thing a switch can''t do is strings... ie CString or char*...

PaladinGLT
PaladinGLT
quote:A switch uses a jump table

This is stated too strongly.

A switch *can* be optimized into a jump table. It requires certain conditions to hold true -- most importantly, the values should all be fairly close and fairly small.

If not, it may get turned into chained if/elses, or potentially a binary tree search.

quote:Switches are preferred to chained-if''s. And virtual functions are preferred to switch blocks.

It doesn''t seem immediately obvious as to why you would use a switch instead of a virtual function, nor how you might replace a switch with a virtual function, unless perhaps you''re doing something vile with RTTI.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
I think VB can do case statements with strings, not just ints...feel free to correct me if I''m wrong
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
quote:Original post by Magmai Kai Holmlor
Perhaps if you''re programing in (Object) Pascal or VB - we like C/C++ here

I prefer C/C++ too. Back when I was taught C in college(back in the stone ages), we were taught that switch/case were treated the same as if/else by the compiler, except for certain restrictions on the switch/case. I took those classes back in 1989, so maybe things have changed since then. I haven''t kept up that much on it until now. I just started getting back into it.


---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
<C# Plug>C# does switches on constant strings</C# Plug>

Once there was a time when all people believed in God and the church ruled. This time is called the Dark Ages.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Arild Fines: Are you working for Microsoft?
quote:Original post by Arild Fines
<C# Plug>C# does switches on constant strings</C# Plug>

So does VB.

This topic is closed to new replies.

Advertisement