Home > Backend Development > C++ > Using range in switch case in C/C++

Using range in switch case in C/C++

王林
Release: 2023-09-01 12:09:02
forward
1727 people have browsed it

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

In C or C, we use switch-case statement. In switch statement we pass some value and using different cases we can check that value. Here we will see that we can use scopes in case statements.

The syntax for using range in Case is as follows -

case low … high
Copy after login

After writing the case, we have to enter the lower value, then a space, then three dots, and then another spaces, and finally the higher value.

In the following program, we will see what is the output of a range-based case statement.

Example

#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;
      }
   }
}
Copy after login

Output

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
Copy after login

The above is the detailed content of Using range in switch case in C/C++. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template