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

折月煮酒 提交于 2019-12-22 04:49:11

问题


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

回答1:


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




回答2:


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



回答3:


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



回答4:


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.




回答5:


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.




回答6:


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;

}



回答7:


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.




回答8:


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




回答9:


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




回答10:


Some C++ compilers still support the syntax today-- 8 years after this question was originally asked. It surprises me.

I learned Pascal in 2012, Pascal do have range notations. Then something encouraged me to try the similar syntax with C++, then it works unexpectedly fabulous.

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


来源:https://stackoverflow.com/questions/2094776/is-it-possible-to-do-the-following-in-a-switch-statement-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!