Switch Statements

Started by
14 comments, last by TrueTom 17 years, 9 months ago
I have learned how to use switch statements with characters for input, but is there a way to do it with numbers or strings?
Advertisement
With integers, yes. With floats, no. With strings, no.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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.
And there isn't even the intention to fix this in C++0x...
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).
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Quote:Original post by medevilenemy
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).


Sorry, but no...

(except floats, of course)
Quote:Original post by TrueTom
Quote:Original post by medevilenemy
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).


Sorry, but no...

(except floats, of course)


Perfect post there, chum. Care to elaborate with us "inferiors"?
daerid@gmail.com
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; }
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.

This topic is closed to new replies.

Advertisement