AI编程助手
AI免费问答

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

王林   2023-09-01 12:09   3952浏览 转载

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

在 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 <h2>输出</h2>
<pre class="brush:php;toolbar:false;">5 in range 1 to 10
4 in range 1 to 10
10 in range 1 to 10
25 in range 21 to 30
60 Exceeds the range
47 Exceeds the range
23 in range 21 to 30
80 Exceeds the range
14 in range 11 to 20
11 in range 11 to 20

C++免费学习笔记(深入):立即学习
>在学习笔记中,你将探索 C++ 的入门与实战技巧!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除