0
Can we add a range in case statement?
Is it possible that we can add a range in case statement?
2 Answers
+ 3
#include <iostream>
using namespace std;
int main() {
int num = 2;
switch(num) {
case 1 ... 10: {
cout << "1 - 10\n";
break;
}
case 11 ... 20: {
cout << "11 - 20\n";
break;
}
default: {
cout << "Range error\n";
}
}
return 0;
}
+ 1
As a side note, the range switch was supported as language extension by GCC and CLang compilers. But I'm not sure if the range switch is now has been included in the standard.
https://stackoverflow.com/questions/36748934/how-can-i-use-ranges-in-a-switch-case-statement-in-c