0

Can we add a range in case statement?

Is it possible that we can add a range in case statement?

27th Oct 2019, 3:31 AM
Amresh
Amresh - avatar
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; }
27th Oct 2019, 4:05 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 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
27th Oct 2019, 4:34 AM
Ipang