Using range in switch case in C/C++ - GeeksforGeeks

22 Sep, 2021
You all are familiar with switch case in C/C++, but did you know you can use range of numbers instead of a single number or character in case statement.
That is the case range extension of the GNU C compiler and not standard C or C++
You can specify a range of consecutive values in a single case label, like this:
case low ... high:
It can be used for ranges of ASCII character codes like this:
 case 'A' ... 'Z':
You need to Write spaces around the ellipses ... . For example, write this:
// Correct  -   case 1 ... 5:
// Wrong -    case 1...5: 
C++
C
// C++ program to illustrate// using range in switch case#include <iostream>using namespace std;int main(){    int arr[] = { 1, 5, 15, 20 };     for (int i = 0; i < 4; i++)    {        switch (arr[i])        {        case 1 ... 6:            cout << arr[i] <<" in range 1 to 6\n";            break;        case 19 ... 20:            cout << arr[i] <<" in range 19 to 20\n";            break;        default:            cout << arr[i] <<" not in range\n";            break;        }    }    return 0;}  //This code is contributed by shivanisinghss2110
// C program to illustrate// using range in switch case#include <stdio.h>int main(){    int arr[] = { 1, 5, 15, 20 };     for (int i = 0; i < 4; i++)    {        switch (arr[i])        {        case 1 ... 6:            printf("%d in range 1 to 6\n", arr[i]);            break;        case 19 ... 20:            printf("%d in range 19 to 20\n", arr[i]);            break;        default:            printf("%d not in range\n", arr[i]);            break;        }    }    return 0;}
Output: 
1 in range 1 to 6
5 in range 1 to 6
15 not in range
20 in range 19 to 20
Time Complexity: O(n), where n is the size of array arr.
Auxiliary Space: O(1)Exercise : You can try above program for char array by modifying char array and case statement.Error conditions: 
low > high : The compiler gives an error message.Overlapping case values : If the value of a case label is within a case range that has already been used in the switch statement, the compiler gives an error message.
low > high : The compiler gives an error message.
Overlapping case values : If the value of a case label is within a case range that has already been used in the switch statement, the compiler gives an error message.
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
subhammahato348
holgerbeer
shivanisinghss2110
cpp-switch
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
Left Shift and Right Shift Operators in C/C++
fork() in C
Command line arguments in C/C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
Inheritance in C++
Constructors in C++