Jump to content
  • Advertisement
Sign in to follow this  
Kenny77

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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"?

Share this post


Link to post
Share on other sites
Do lookups in a map instead:


std::string s;
// populate s here

// function style
// old
if (s == "foo") { foo(); }
else if (s == "bar") { bar(); }
// lots more cases here
else { do_default(); }

// new
// Note you need more sophisticated stuff in order to deal with member functions
typedef void(*func)();
typedef std::map<std::string, func> funcmap;
funcmap funcs;
funcs["foo"] = foo;
funcs["bar"] = bar;
// lots more cases here

funcmap::iterator it = funcs.find(s);
if (it == funcs.end()) { do_default(); }
else { it->second(); }

// For simple assignments to variables:
// old
std::string s;
// populate s here
if (s == "foo") { x = 0; }
else if (s == "bar") { x = 1; }
// lots more cases here
else { x = -1; }

// new
typedef void(*func)();
typedef std::map<std::string, int> funcmap;
funcmap funcs;
funcs["foo"] = 0;
funcs["bar"] = 1;
// lots more cases here

funcmap::iterator it = funcs.find(s);
if (it == funcs.end()) { x = -1; }
else { x = it->second; }

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!