在 case 中使用范围的语法如下 -
case low … high
写完 case 后,我们必须输入较低的值,然后是一个空格,然后是三个点,然后是另一个空格,最后是较高的值。
在下面的程序中,我们将看到什么是基于范围的 case 语句的输出。
示例#include <stdio.h>main() { int data[10] = { 5, 4, 10, 25, 60, 47, 23, 80, 14, 11}; int i; for(i = 0; i < 10; i++) { switch (data[i]) { case 1 ... 10: printf("%d in range 1 to 10\n", data[i]); break; case 11 ... 20: printf("%d in range 11 to 20\n", data[i]); break; case 21 ... 30: printf("%d in range 21 to 30\n", data[i]); break; case 31 ... 40: printf("%d in range 31 to 40\n", data[i]); break; default: printf("%d exceeds the range\n", data[i]); break; } }}
输出5 in range 1 to 104 in range 1 to 1010 in range 1 to 1025 in range 21 to 3060 exceeds the range47 exceeds the range23 in range 21 to 3080 exceeds the range14 in range 11 to 2011 in range 11 to 20
以上就是在c/c++中使用范围在switch case中的详细内容。
