Is it possible to do the following in a switch statement - C ++?

I participate in programming in my second class of OOP, and I have a simple question that I could not find the answer on the Internet, if it is there, I apologize.

My question is:

Is it possible to have Boolean conditions in switch statements?

Example:

switch(userInputtedInt)
{
    case >= someNum && <= someOtherNum
    break;
    // Is this possible?
}
+6
source share
11 answers

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 logical condition, you should use the if / else block

+12
source

No, this usually refers to the expression if:

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

, switch:

switch (x) {
    case 1:
        // handle 1
        break;
    default:
        if ((x >= 2) && (x <= 20)) { ... }
}
+7

, , , ++ . :

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

- C ++ , , . , , . 1 100, 90-100 , 80-89 .., 10, .

+3

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

, if-else.

+1

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

0

. , ,

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

}
0

++ .

, , :

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

.

0

, - , case. case (: #define CASE0 case 0: #define CASE1 case 1:...)

, ... !;)

0

:

6.4.2 switch [stmt.switch]

[...] switch case :

case constant-expression :

(5.19).

0

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

I recognized Pascal in 2012, Pascal has a range designation. Then something prompted me to try a similar syntax with C ++, then it works unexpectedly fabulously.

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

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

In addition, the source code is attached as follows:

#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
source

All Articles