Sign in to follow this  

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

This topic is 4300 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
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
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
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.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Sure you can.

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


(else is left as an exercise to the reader.)


Or better yet, since true = 1 and false = 0:

#define if(b) for (unsigned __i__ = 0; __i__ < (b); __i__ += 1)

#include <iostream>
int main() {
if (true == false)
std::cout << "WTF?";
}



EDIT: Ofcourse that is, if "if" isn't reserved in C++++.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Sneftel
If you were hellbent on using a switch statement? just use a goto as a multilevel break (one of the few uncontroversial uses of goto).



There are still people who are rabidly against ANY use of GOTO....
(similar to the only one RETURN crowd....)

GOTOs are also good for error processing -- where the rest of a routine becomes invalid due to parameter conditions etc... Having it jump down to a well labeled cleanup section can be very clear and obvious (dont know why the 'GOTO' nazis cant see this.....)


For the original poster -- theres no real point in using the SWITCH if you have only 2 desired results (you might as well use an IF ELSE statement).


Performance-wise a SWITCH can be compiled into a jump table (using the ordinal as an ofset so that it wouldnt have to execute repeated if/elseif/elseif.... when you have alot of cases). The code might not be any better (depending on how good the optimization is) when your CASEs span large gaps (ie- 1,2,3 101,102,103, 2534, 44565...). Using ENUMs usual works most optimally (packed into one linear range ...)



Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
GOTOs are also good for error processing -- where the rest of a routine becomes invalid due to parameter conditions etc... Having it jump down to a well labeled cleanup section can be very clear and obvious (dont know why the 'GOTO' nazis cant see this.....)


Because with exceptions and RAII you generally don't *need* an explicit cleanup section? I guess that would mean this is one of the controversial uses of GOTO. [smile]

Share this post


Link to post
Share on other sites

This topic is 4300 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this