What is the point of a switch statement?

closed account (jwC5fSEw)
I understand what it is and how to use it, but I don't understand why you'd want to. It's more restrictive than an if statement and uses odd syntax. Why not just use an equivalent if statement?
it's preferable to else/if chains when there's lots of possibilities because it can often be compiled to a jump table and thus have greater performance.

Where an if/else chain will test every condition until it finds a match, a switch might be able to just jump to the right code without having to do any tests.

I've used it a lot in emulators where you have to check opcodes. In my NES emu I had 256 possibilities. A switch is great for something like that. Can you imagine a if/else chain with 256 checks?
IIRC, there is also a limit to how long an if/else if chain can get.
closed account (jwC5fSEw)
Can you imagine a if/else chain with 256 checks?


Okay that would be pretty cumbersome. But why does the switch only accept numbers?
Because it can compile to a jump table, so it can only do integers... because the integer has to index the jump table.

Basically the idea is something like this:

1
2
3
4
5
6
7
8
9
10
switch(foo)
{
case 0:
  DoSomething();
  break;

case 1:
  DoSomethingElse();
  break;
}

---- could compile to something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
const address jumptable[2] = { case0, case1 };

goto jumptable[ foo ];

case0:
  DoSomething();
  goto end;

case1:
  DoSomethingElse();
  goto end;

end:
Wow I didn't use it with strings yet, but I never knew it doesn't work with strings..
In C# I could switch almost anything, bool -> string..
So anyways, if i have something like 5-6 possibilities, Is it better to use a switch or a sequence of if/elses?
The switch as used in C++ is the same as that used in C - I can't see any reason (but I'm no expert) why it cannot be updated/expanded in C++ to include user defined types like classes.
As the case but is really an equality test - the class could overload the == operator appropriately.
For those things like comparing C strings then a global overload function could be provided by the programmer as well.
I'm not talking about a one/two possibilities of a string, or I would have never asked that, I'm talking about almost 5-6 possibilities where it looks to me cumbersome to sequence up if/elses, and I wasn't talking about C also :P, I was talking about C# anyways, so this can only accept integers.. and what about enums, don't they count as integers with variables?
@SimpleButPerfect
I was just making a general comment about the theoretical possibilities of expanding the C++ switch beyond integers.
Last edited on
http://www.cplusplus.com/forum/general/11460/#msg54095
(Scroll down for select on a string)

:-)


[edit] Well, there is no switch statement in there for that example, but the point was turning a string into an integer index, which can be used with a switch...
Last edited on
To switch on a string, I use map<string, functor>. It trades off some flexibility (e.g. no missed "breaks", no access to locals) for better encapsulation.
Topic archived. No new replies allowed.