C++ Switch Statements

C++ Switch statements: The C++ switch statements allows us to test multiple conditions for the if statement. These statements are the substitute for long if statements that compare a variable to several integral values. And in the other sense, we can also it as a  multi way “branching statement”. It is also easy to dispatch execution to different parts of the code based on the value of the expression.

C++ Switch statements with Syntax & Example

Switch in the control statement allows a value to change the control of the execution of the program. And in this statement, every value is called a case and the variable will be checked on and checks each case. While the expression provided in the switch should result in a constant value. Otherwise, it would not be valid.

Syntax:

switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn’t match any cases
}



[Flowchart for switch statement]

NOTE:

  • The default statement is optional. Even if the switch statement does not have a default statement. It will be running without any problem.
  • Duplicate case values are not allowed.
  • The break statement is used inside the switch to terminate a statement sequence.
  • When the break statement is reached, the flow of control jumps to the next line following the switch statement.
  • The flow of control will fall through to subsequent cases until a break is reached.

C++ Switch Statement Example:

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
    char c;
    cout <<"Enter a alphabet:";
    cin >> c;
    c=tolower(c);
    switch (c)
    {
        case 'a':
            cout <<"You entered a, its a vowel"<<endl;
        case 'e':
            cout <<"You entered e, its a vowel"<<endl;
        case 'i':
            cout <<"You entered i, its a vowel"<<endl;
        case 'o':
            cout <<"You entered o, its a vowel"<<endl;
        case 'u':
            cout <<"You entered u, its a vowel"<<endl;
        default:
            cout <<"You entered consonant";
    }
    getch();
    return 0;
}

Output:

input
a
Enter an alphabet: a
You entered a, its a vowel
You entered e, its a vowel
You entered i, its a vowel
You entered o, its a vowel
You entered u, its a vowel
You entered consonant

Example 2

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
 float percent;
 int x;
 cout<<"Enter your percentage: ";
 cin>>percent;
 cout<<"You scored "<<percent<<"%"<<endl;   
  x = percent/10;
 switch (x)
 {
  case 10:
  case 9:
  case 8:
      cout<<"You have passed with distinction";
      break;
  case 7:
  case 6: 
        cout<<"You have passed with first division";
        break;
  case 5:
       cout<<"You have passed with second division";
       break;
  case 4:
      cout<<"You have passed with third division";
      break;
  default:
      cout<<"Sorry: You have failed";
    }
    getch();
    return 0;
}

Output:

Enter your percentage: 70
You scored 70%
You have passed with first division