Public Group

# Switch Statements

This topic is 4486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have learned how to use switch statements with characters for input, but is there a way to do it with numbers or strings?

##### Share on other sites
With integers, yes. With floats, no. With strings, no.

##### Share on other sites
You can do it with any numeric integral type. You can also do it with strings in C# and PHP, in C or C++ you would have to resort to a sequence of else if.

Darn, my computer won't post this message! Damn you to hell, computer!

EDIT: integral! I swear I meant integral.

##### Share on other sites
The way I did it with strings was a series of an if and else ifs. like so:

			if(!strcmp(buffer, "most likely option"))			{ ... } //Do thing for first			else if(!strcmp(buffer, "second most likely option"))			{ ... }			else if(!strcmp(buffer, "third most likely option"))			{ ... }

Thats how I did it. I would also order the test strings in the order of how likely it will be selected, it should help imrove the speed somewhat.

##### Share on other sites
And there isn't even the intention to fix this in C++0x...

##### Share on other sites
There isnt any need for this to be "fixed". Switch statements are usually used only in the case of the simplest comparisons. For a complex comparison like a floating point number or a string, etc, it just makes more sense to use the if and else statements.

I, for one, have never really had the need or desire to use strings in a switch (except perhaps about 6 or 7 years ago when I learned what a switch was).

##### Share on other sites
Quote:
 Original post by medevilenemyThere isnt any need for this to be "fixed". Switch statements are usually used only in the case of the simplest comparisons. For a complex comparison like a floating point number or a string, etc, it just makes more sense to use the if and else statements.I, for one, have never really had the need or desire to use strings in a switch (except perhaps about 6 or 7 years ago when I learned what a switch was).

Sorry, but no...

(except floats, of course)

##### Share on other sites
Quote:
Original post by TrueTom
Quote:
 Original post by medevilenemyThere isnt any need for this to be "fixed". Switch statements are usually used only in the case of the simplest comparisons. For a complex comparison like a floating point number or a string, etc, it just makes more sense to use the if and else statements.I, for one, have never really had the need or desire to use strings in a switch (except perhaps about 6 or 7 years ago when I learned what a switch was).

Sorry, but no...

(except floats, of course)

Perfect post there, chum. Care to elaborate with us "inferiors"?

##### Share on other sites
Do lookups in a map instead:

std::string s;// populate s here// function style// oldif (s == "foo") { foo(); }else if (s == "bar") { bar(); }// lots more cases hereelse { do_default(); }// new// Note you need more sophisticated stuff in order to deal with member functionstypedef void(*func)();typedef std::map<std::string, func> funcmap;funcmap funcs;funcs["foo"] = foo;funcs["bar"] = bar;// lots more cases herefuncmap::iterator it = funcs.find(s);if (it == funcs.end()) { do_default(); }else { it->second(); }// For simple assignments to variables:// oldstd::string s;// populate s hereif (s == "foo") { x = 0; }else if (s == "bar") { x = 1; }// lots more cases hereelse { x = -1; }// newtypedef void(*func)();typedef std::map<std::string, int> funcmap;funcmap funcs;funcs["foo"] = 0;funcs["bar"] = 1;// lots more cases herefuncmap::iterator it = funcs.find(s);if (it == funcs.end()) { x = -1; }else { x = it->second; }

##### Share on other sites
Why wouldn't you want switch to work with more types? C#'s switch works on strings, it surely comes in handy sometimes. O'Caml/SML/Haskell use switch-like statements for pattern matching. Lisp has cond, the most general switch possible, which works with predicates (but then again can't be optimised by the compiler). And why shouldn't it also work on floating point values? Just make it smart enough so that it knows it has to work with epsilons for comparisons.

Of course, it depends on where to draw the line between what is a switch and what isn't.

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 20
• 14
• ### Forum Statistics

• Total Topics
632950
• Total Posts
3009378
• ### Who's Online (See full list)

There are no registered users currently online

×