Jump to content
  • Advertisement
Sign in to follow this  
MrPigpen

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

This topic is 4581 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

Hey everyone, I'm in programming languages class and the instructor just asked if there is any situation where you can't use a switch statement and can only use if statements. So far I've been able to write everything from any kind of if statement example into code in a switch statement. Anyone have any ideas of a situation that you HAVE to use an if and it is impossible to use a switch?? I don't think there is...

Share this post


Link to post
Share on other sites
Advertisement
The expression in the switch statement must be an integral type - char, short, int, long, enumeration, etc.

It can't be used with floats, strings, etc.

Share this post


Link to post
Share on other sites
You can only use switch for integer types ( int, short, long, char ) and enum's ( which are considered integers ). So floating point types ( float, double ) and strings ( both char* and std::string ) won't work.

Edit: Too slow :(

Share this post


Link to post
Share on other sites
//Well, how I did them was I changed


If(stringA == stringB)
cout<<"They are the same";


//to this switch


bool temp;

temp = (stringA == stringB);

switch(temp){
case 1:
cout<<"They are the same"
}


//Basically that's how I did it, was create a temporary boolean value and did the compare statement which would put either true of false (1 or 0) into the temp variable and did the switch on that...is that valid?

Share this post


Link to post
Share on other sites
It's valid, but it's very stupid. Is there a reason you wish to avoid using if? A switch (with enough case labels) gets compiled into a jump table, and an if gets compiled into a jump. You don't gain or lose anything for a single test (actually, you lose because of your temporary).

Share this post


Link to post
Share on other sites
switch(stringA == stringB){
case 1:
cout<<"They are the same"
}


In case you really want to use switch without a temporary variable, but that switch doesn't make sense anyway.

Share this post


Link to post
Share on other sites
No, it doesn't make sense, haha. But I'm not trying to make the switch make sense, I'm just trying to make sure ANYTHING you can do in an if statement, you can do in a switch. For example, lets say they come out with a C++++ language or something that is C++, word for word, except there are no if statements at all in this new language. Could you do everything with this new language that you could with c++ by using switch statements for the logic instead of the ifs which no longer exist? That's all I'm trying to ask.

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!