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
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.
#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 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
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!