Can I map a range instead of a single value in a C ++ instruction?

I am new to programming. Can I use <, >in the case of a switch?

For instance,

 ...
 ...
 ...
 int i;
 cin>> i;
 ...
 ...
 switch(i){
 case 20<i<35
 ...
+4
source share
1 answer

C ++ does not offer syntax switchfor mapping ranges.

When the ranges are relatively small, you can put mark labels and rely on failure:

switch(i) {
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    case 25: doSomething();
             break;
    case 26:
    case 27:
    case 28:
    case 29: doSomethingElse();
             break;
    ...
}

For medium-sized ranges (1000 elements or so), you can use a vector of function objects to send to specific logic, but this requires a lot more work than writing a simple statement switch.

For large ranges, the best choice is the operator chain if- else.

+7
source

All Articles