Switch Case in C++

Switch-Case in C++

On this page we will  discuss about switch-case in C++. Switch case allows you to select the required block of code  from an available block of codes.Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it
Switch Case in C++

Switch Case in C++ Language

  • First, the compiler searches the switch expression, top to bottom from available case labels
  • If a case label is matched with the switch expression then body under case label gets executed, then all other case labels get skipped
  • If no case label is matched with switch expression body under default get executed
  • Remember, only one case label gets executed from available case labels,  i.e  case label that is matched with the switch expression

Syntax:

switch (expression)
{ case constant1: //code break; case constant2: // code break; . . . default: }

C++ program to demonstrate switch case

Run
#include<iostream> 
using namespace std;
int main ()
{
  char oper = '+';
  float num1 = 8, num2 = 3;
  cout << "Number 1 = " << num1 << endl;
  cout << "Number 2 = " << num2 << endl;
  cout << "operator = " << oper << endl;
  cout << "result: ";
  switch (oper)
    {
    case '+':
      cout << num1 << " + " << num2 << " = " << num1 + num2;
      break;
    case '-':
      cout << num1 << " - " << num2 << " = " << num1 - num2;
      break;
    case '*':
      cout << num1 << " * " << num2 << " = " << num1 * num2;
      break;
    case '/':
      cout << num1 << " / " << num2 << " = " << num1 / num2;
      break;
    default:

      cout << "Error! The operator is not correct";
      break;
    }

  return 0;
}

Output

Number 1 = 8
Number 2 = 3
operator = + result: 8 + 3 = 11

Points to Remember:

  • Using break is optional but it results in the execution of all case labels from matched case label
Run
#include<iostream>
using namespace std;
int main()
{
  switch(2)
  { 
     case 1: cout<<"Laxmi\n";
     case 2: cout<<"trishaank\n";//matched case label
     case 3: cout<<"rishvik\n";  
     deafult: cout<<"vijay";
   }
}
Output:
trishaank
rishvik
vijay
Run
#include<iostream>  
using namespace std;
int main ()
{
  switch (2)
    {
    case 1:
      cout << "Laxmi\n";
      break;
    case 2:
      cout << "trishaank\n";	//matched case label 
      break;
    default:
      cout << "vijay";
      break;
    case 3:
      cout << "rishvik\n";
    }
}
Output:
trishaank
Run
#include<iostream>
using namespace std;
int main ()
{
  switch (2)
    {
    case 1:
      cout << "Laxmi\n";
      break;
    case 1 + 1:		//same as case 2
      cout << "trishaank\n";	//matched case label 
      break;
    default:
      cout << "Vijay";
      break;
    case 1 + 1 + 1:
      cout << "rishvik\n";
    }
}

Output:

trishaank

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

2 comments on “Switch Case in C++”