Switch statement in C++

In this tutorial, we are going to learn about one of the basic statements used in C++ language that is switch statement in C++. If you are searching for syntax and a complete explanation on this, then you are at right place. Here I am providing you this along with example program.

Also, read

Convert the First letter of each word to uppercase in C++

Switch statement in C++

The switch statement allows the user to choose between different statements of the code. In other words we can say that this provide some options to the user to choose. You can program it accordingly so that it will do the required task on the basis of the choice.



Let’s see the syntax.

switch(x){
case 1: //execute if value of x is 1; break;
case 2: //execute if value of x is 2; break;
case 3: //execute if value of x is 3; break;
default:// executes if all above mentioned cases fails;
}

Important notes

  • The value of the variable inside switch statement should be constant.
  • Every case must be unique without any duplicate cases, else there will be an error.
  • Break statement used at the end of each cases is used to moves the compiler directly to the end of switch case after the success case execution. This is optional but it is always a good practice using it.
  • The default statement is executed if all the cases fail. However it is also optional, the compiler will run further lines of the program without any error in such case.
  • Nesting switch statements is also valid. You can do this but it makes the program complex.

Example Of Switch Case in C++

I have written and tested all programs in Turbo C++ v 4.0.

#include <iostream.h>
#include<conio.h>

int main()
{
    int x=0;
    cout<<"Enter your choice for switch statement from 1 to 3: ";
    cin>>x;
    switch(x){
  case 1: cout<<"You have selected case 1";break;
  case 2: cout<<"You have selected case 2";break;
  case 3: cout<<"You have selected case 3";break;
  default: cout<<"Invalid choice";
    }
    getch();
    return 0;
}

Output

Enter your choice for switch statement from 1 to 3:1
You have selected case 1

Enter your choice for switch statement from 1 to 3:5
Invalid choice

We can also use character variable for switch statement instead of integer value. Example program illustration this is given below.

#include <iostream.h>
#include<conio.h>

int main()
{
    char ch;
    cout<<"Enter your choice for switch statement from a to c: ";
    cin>>ch;
    switch(ch){
  case 'a': cout<<"You have selected case a";break;
  case 'b': cout<<"You have selected case b";break;
  case 'c': cout<<"You have selected case c";break;
  default: cout<<"Invalid choice";
    }
    getch();
    return 0;
}

Output

Enter your choice for switch statement from a to c: b
You have selected case b

Enter your choice for switch statement from a to c: g
Invalid choice

Please leave your queries in the comment section below.

Leave a Reply

Your email address will not be published. Required fields are marked *