Any situation where you can't use a Switch statement in C++

Started by
32 comments, last by moeron 18 years, 1 month ago
The switch statement is *especially* suited to enumerations. It makes alot of sense then. If you think it's useless, or you don't understand why you can't use it with strings, then (no offense) you probably don't fully grasp it's intended purpose (that is, handling multiple *cases* depending on some state), and you might want to look into std::map, which will work with strings.

Looking for a serious game project?
www.xgameproject.com
Advertisement
None taken. But yes, I understand switch statements are better suited for multiple cases in that they are better for readability than doing a massive else if statement. The question "Could you still have all the current functionality in C++ if the 'if' statements were completely eliminated?" just came up in Programming Languages class earlier today. I'm not saying one is better than the other or we should do away with one or which one is suited best for string or integers or variants or potato chips, lol. I am sorry if this sounds like a noob question. It's just something I started thinking about. Thanks though for all of the replys, this is definitely one of the (if not THE) most helpful forums I've ever been on.
Sure you can.

#define IF(x) for(int _i=0; _i<(x)?1:0; _i++)


(else is left as an exercise to the reader.)
Of course, you can also replace an if with a while too...
while (stringA == stringB) {    //Do Something    break;}
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by Omega147
Yeah, switch statements will only accept int's, bool's, and char's.


not true. you can do:

enum list
{
stuff
other_stuff
}

switch(other_list)
{
case stuff:

case other_stuff:
Mitchen Games
Quote:Original post by bballmitch
Quote:Original post by Omega147
Yeah, switch statements will only accept int's, bool's, and char's.


not true. you can do:

enum list
{
stuff
other_stuff
}

switch(other_list)
{
case stuff:

case other_stuff:


Um. You do know that enum values are ints, don't you?
Well, in principle, a language is Turing complete with just one conditional branch and one loop construct, right? Anything you can do with 'for', you can do with 'while' (or indeed with a switch and a goto), so I don't see why you shouldn't be able to replace ifs with switches.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Quote:Original post by King of Men
Well, in principle, a language is Turing complete with just one conditional branch and one loop construct, right? Anything you can do with 'for', you can do with 'while' (or indeed with a switch and a goto), so I don't see why you shouldn't be able to replace ifs with switches.


You don't even need that. Lambda calculus is Turing complete, and it has nothing but higher-order functions.
Yes, infact I do know a situation that only works for if loops.

Let's say you have to test a bit array for certain bits.

You can only sort through it once, because if a bit is 1, then it is flipped to 0, and vice versa on certain bits.

You can run down a series of 8 if statements, we'll say, and then were done.

However, in a switch statement, you can't do this. Using "break" will exit the loop, and if you repeat, your results will be messed up. If you drop down, you could flip bits that you don't want to flip.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
Using "break" will exit the loop

No it won't. break exits only from the most deeply nested loop or switch.

This topic is closed to new replies.

Advertisement