开发者

Is It Possible To Do The Following In A Switch Statement - C++?

开发者 https://www.devze.com 2022-12-17 23:14 出处:网络
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.开发者_如何学编程

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?
}


No this is not possible in C++. Switch statements only support integers and characters (they will be replaced by their ASCII values) for matches. If you need a complex boolean condition then you should use an if / else block


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;
}


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)) { ... }
}


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.


Or you can perhaps do this

switch((userInputtedInt >= someNum) && (userInputtedInt <= someOtherNum))
{
case true:
     //do something
     break;
case false: 
     //something else
     break;
}

But that's just down-right terrible programming that could be handled with if-else statements.


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;

}


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.


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! ;)


The standard does not allow for this:

6.4.2 The switch statement [stmt.switch]

[...] Any statement within the switch statement can be labeled with one or more case labels as follows:

case constant-expression :

where the constant-expression shall be an integral constant expression (5.19).


Some C++ compilers still support range notations today, 8 years after this question was originally asked. It surprised me.

I learned Pascal in 2012, Pascal do have range notations. So it encouraged me to try the similar syntax in C++, then it worked unexpectedly fabulously.

The compiler on my laptop is g++ (GCC) 6.4.0 (from Cygwin project) std=c++17

There is a working example, which I wrote in hurry. repl.it

In addition, the source code is attached as follow:

#include <iostream>
using namespace std;
#define ok(x) cout << "It works in range(" << x << ")" << endl
#define awry cout << "It does\'t work." << endl

int main() {
    /*bool a, b, c, d, e, f, g;
    switch(true) {
        case (a): break;            These does not work any more...
        case (b and c): break;
    }*/
    char ch1 = 'b';
    switch(ch1) {
        case 'a' ... 'f': ok("a..f"); break;
        case 'g' ... 'z': ok("g..z"); break;
        default: awry;
    }
    int int1 = 10;
    switch(int1) {
        case 1 ... 10: ok("1..10"); break;
        case 11 ... 20: ok("11..20"); break;
        default: awry;
    }

    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消