- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
- Related Articles
- Switch case statement in C
- Explain nested switch case in C language
- Converting digits to word format using switch case in C language
- What is difference between using if/else and switch-case in C#?
- Write a C program of library management system using switch case
- C program to find the areas of geometrical figures using switch case
- Switch case in Arduino
- Write a C program for electing a candidate in Elections by calling functions using Switch case
- Switch Case in Python (Replacement)
- Switch case calculator in JavaScript
- String in Switch Case in Java
- C program to print area of triangle, square, circle, rectangle and polygon using switch case.
- How to use case-insensitive switch-case in JavaScript?
- The String in Switch Case in Java
- C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case

Advertisements