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:
1 in range 1 to 6 5 in range 1 to 6 15 not in range 20 in range 19 to 20
Exercise : You can try above program for char array by modifying char array and case statement.
- low > high : The compiler gives with 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 contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Nested switch case
- Output of C programs | Set 30 (Switch Case)
- Menu-Driven program using Switch-case in C
- Data type of case labels of switch statement in C++?
- Switch Statement in C/C++
- Nested switch statement in C++
- C++17 new feature : If Else and Switch Statements with initializers
- Interesting facts about switch statement in C
- Print individual digits as words without using if or switch
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- C Program for Lower Case to Uppercase and vice-versa in a file
- Range-based for loop in C++
- How to delete a range of values from the Set using Iterator
- Calculate range of data types using C++
- Generating random number in a range in C
- Print prime numbers in a given range using C++ STL
- How to check whether a number is in the range[low, high] using one comparison ?
- How to delete a range of values from the List using Iterator
- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- What happen when we exceed valid range of built-in data types in C++?