Example of Using Range in C++ Switch Case

Below is the implementation of the above topic:

C++




// C++ Program to implement
// Using Range in Swtich Case
#include<iostream>
using namespace std;
  
// main function
int main()
{
    int a=8;
  
      // Switch Case
    switch(a)
    {
        // Range added to Switch case
        case 4 ... 10:
            cout<<"a is range 4 to 10"<<endl;
            break;
    }
}


Output :

a is range 4 to 10

Note:

  1. We have to exactly use 3 dots other wise it will give us error while compilation.
  2. If low>high then also we will get an error

Using Range in C++ Switch Case

In C++, we generally know about the switch case which means we give an attribute to the switch case and write down the cases in it so that for each case value we can make desired statements to get executed. We can also define the cases with a range of values instead of a single value.

Prerequisites: Switch Case in C++

Similar Reads

Switch Case with Range

Using range in switch case means in the above scenario we used to check only one value to the given attribute but now we can check a range of values to the given attribute in a single case...

Example of Using Range in C++ Switch Case

Below is the implementation of the above topic:...

Contact Us