Why Switch/Case and not If/Else If?
Posted
by OB OB
on Stack Overflow
See other posts from Stack Overflow
or by OB OB
Published on 2009-06-22T17:26:18Z
Indexed on
2010/03/27
9:53 UTC
Read the original article
Hit count: 305
This question in mainly pointed at C/C++, but I guess other languages are relevant as well.
I can't understand why is switch/case still being used instead of if/else if. It seems to me much like using goto's, and results in the same sort of messy code, while the same results could be acheived with if/else if's in a much more organized manner.
Still, I see these blocks around quite often. A common place to find them is near a message-loop (WndProc...), whereas these are among the places when they raise the heaviest havoc: variables are shared along the entire block, even when not propriate (and can't be initialized inside it). Extra attention has to be put on not dropping break's, and so on...
Personally, I avoid using them, and I wonder wether I'm missing something?
Are they more efficient than if/else's? Are they carried on by tradition?
© Stack Overflow or respective owner