您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在C/C++中使用范围在switch case中

2024/3/6 13:57:26发布19次查看
在 c 或 c++ 中,我们使用了 switch-case 语句。在 switch 语句中,我们传递一些值,并使用不同的情况,我们可以检查该值。在这里我们将看到我们可以在 case 语句中使用范围。
在 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中的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product