
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using Range in Switch Case in C/C++
In C or C++, we have used the switch-case statement. In the switch statement we pass some value, and using different cases, we can check the value. Here we will see that we can use ranges in the case statement.
The syntax of using range in Case is like below −
case low … high
After writing case, we have to put lower value, then one space, then three dots, then another space, and the higher value.
In the following program, we will see what will be the output for the 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; } } }
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
Advertisements