views:

312

answers:

9

Hi there,

I am a programming student in my second OOP class, and I have a simple question that I have not been able to find the answer to on the internet, if it's out there, I apologize.

My question is this:

Is it possible have Boolean conditions in switch statements?

Example:

switch(userInputtedInt)
{
    case >= someNum && <= someOtherNum
    break;
    // Is this possible?
}
+8  A: 

No this is not possible in C++. Switch statements only support simple integer matches. If you need a complex boolean condition then you should use an if / else block

JaredPar
`Select Case` in VB is so easier in this case...
Paulo Santos
A: 

/////////////// Nope

Andrew Noyes
In C++ shouldn't that be /////////////// Nope ?
Martin Beckett
/////////////// Right you are!
Andrew Noyes
A: 

This isn't possible. The closest you can some, if the values are reasonably close together is

switch(userInputtedInt)
{
    case someNum:
    case someNum+1:
    // ... 
    case someOtherNum:
    break;

}
KeithB
A: 

C++ does not support that.

However, if you are not concerned with writing portable, standard code some compilers support this extended syntax:

switch(userInputtedInt)
{
    case someNum...someOtherNum:
    break;
}

Those values must be constant.

Shmoopty
What compilers support that syntax?
David Rodríguez - dribeas
I think I recall Metrowerks supporting it. Perhaps others? It is, of course, merely shorthand for typing several adjacent case labels.
Shmoopty
+3  A: 

No, this is usually the purview of the if statement:

if ((userInputtedInt >= someNum) && (userInputtedInt <= someOtherNum)) { ... }

Of course, you can incorporate that into a switch statement:

switch (x) {
    case 1:
        // handle 1
        break;
    default:
        if ((x >= 2) && (x <= 20)) { ... }
}
paxdiablo
+1 for "purview".
markh44
+3  A: 

It's not possible directly -- a C or C++ switch statement requires that each case is a constant, not a Boolean expression. If you have evenly distributed ranges, you can often get the same effect using integer division though. e.g. if you have inputs from 1 to 100, and want to work with 90-100 as one group, 80-89 as another group, and so on, you can divide your input by 10, and each result will represent a range.

Jerry Coffin
+6  A: 

As others have said you can't implement this directly as you are trying to do because C++ syntax doesn't allow it. But you can do this:

switch( userInputtedInt )
{
  // case 0-3 inclusve
  case 0 :
  case 1 :
  case 2 :
  case 3 :
    // do something for cases 0, 1, 2 & 3
    break;

  case 4 :
  case 5 :
    // do something for cases 4 & 5
    break;
}
John Dibling
This is interesting, It's a great use of C++'s support for implicit fall through.
Alex
@Alex: Actually, although this is supported by C++ I have found it is usually less expressive and less useful than you might at first think. Switch statements like this tend to become huge, making them difficult to read, maintain and debug. I will typically use ifs before using a switch like this. Or better yet I'll do something else entirely.
John Dibling
A: 

No. And trying it is the best way to learn.

kirk.burleson
A: 

If you fancy the preprocessor you could write some kind of macro that auto-expands to the number of case statement required. However that would require a lengthly file with pretty much all case statements (ex: #define CASE0 case 0: #define CASE1 case 1: ...)

You shouldn't go there but it's fun to do...for fun! ;)

Francis Boivin