C/C++之switch范围判断

1 switch范围判断用法,使用’…’符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main(int argc, char *argv[])
{
int i = 5;
switch (i) {
case 0 ... 3:
std::cout<<"0-3: "<<i<<std::endl;
break;
case 4 ... 6:
std::cout<<"4-6: "<<i<<std::endl;
break;
case 7 ... 9:
std::cout<<"7-9: "<<i<<std::endl;
break;
case 10:
std::cout<<"10"<<i<<std::endl;
default:
break;
}
return 0;
}

2 输出

4-6: 5