Archived

This topic is now archived and is closed to further replies.

the_grip

Switch vs. If/Else

Recommended Posts

Rudan    158
Well, I guess a switch could be a bit faster to use instead of a lot of if, since you usually break out of the switch statement when a case is true so the computer wont have to check the other cases. But I''m just guessing here. And there may be some other differences too...


-----------------------------
"problems have solutions
a lifetime of fucking things up fixed in one determined flash"
- The Downward Spiral, NIN

Share this post


Link to post
Share on other sites
thuned    122
i don't know about the speed, but i just use whatever i think is neater.
and logically
    
if ()
{
}
else
{
}

should be faster than

if ()
{
}
else if ()
{
}

although i haven't done any testing on it.


Edited by - thuned on January 30, 2002 11:24:38 AM

Share this post


Link to post
Share on other sites
CaptainJester    523
A switch/case statement is exactly the same as an if/else set. The switch/case was introduced to provide a neater option then a large string of if/else statements. The compiler treats them the same way. You have to make sure you use if...else if...else if...else, this way you drop out of the set as soon as the condition is met.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

Share this post


Link to post
Share on other sites
DrPizza    160
If your switch is exhausting and uses a small data set, your compiler should generate a jump table.

If it isn''t, it should generate chained if/elses instead.

Share this post


Link to post
Share on other sites
Kylotan    9870
Yeah, switches are probably easier for the compiler to optimise, as it knows that all the following blocks are based around the same condition, and that the condition is only evaluated once. Apart from that they''re generally the same (although an else block is not the same as an else if block, for example).

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
EvilCrap    134
in c++ the use of switch and if are fundamentally different.
switches use const arguments, and ifs dont.

  
switch(y)
{//not allowed

case x:
// allowed

case NULL: //constant

}


so you may not be able to replace any if/else with a switch, but, switches and ifs can often be implimented interchangably. and imo switches are alot more elegant.

Mark Duffill: switch does a cmp for each case, just like an if/else does.

Share this post


Link to post
Share on other sites
KingSalami    122
EvilCrap is the only one to get it right. switch...case statements only evaluate integer expressions, if an if else can be replaced by a switch statement they are a bit speedier

Share this post


Link to post
Share on other sites
OldGuy    122
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.

Share this post


Link to post
Share on other sites
Shannon Barber    1681
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

Share this post


Link to post
Share on other sites
DrPizza    160
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.

Share this post


Link to post
Share on other sites
CaptainJester    523
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

Share this post


Link to post
Share on other sites
Arild Fines    968
<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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Arild Fines
<C# Plug>C# does switches on constant strings</C# Plug>

So does VB.

Share this post


Link to post
Share on other sites