Switch Case Statement in C++

Introduction

Switch case is an alternative in C++ and other programming languages when you need to compare an expression to a constant value and don’t want to use multiple if-else statements.

Let’s understand the syntax of switch-case statements

Syntax

The following snippet is the syntax for switch case statements

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    case value3:
        // code
        break;
}

Properties of switch case

  1. The expression within the switch statement should return a constant value otherwise it would return an error.
  2. When no case is matched, one can create a default case to execute a code block.
  3. A break statement is used in a switch case to break out of the case block and execute the lines that follow the switch-case statement.

Note:

When a break statement is missing from a case block and that case matches, it would execute every case after that case.

Let’s understand this with an example:

C++ Code

#include<iostream>
using namespace std;

int main() {
  int x = 2;
  switch (x) {
  case 1:
    cout << "Choice is 1"<<”\n”;
    // break;
  case 2:
    cout << "Choice is 2"<<”\n”;
    // break;
  case 3:
    cout << "Choice is 3"<<”\n”;
    // break;
  default:
    cout << "Choice other than 1, 2 and 3"<<”\n”;
    // break;
  }
}

In the above program, case 2 matches with the given expression, since no condition is having a break statement, it would execute every case after case 2. Hence the output will be – 

Choice is 2

Choice is 3

Choice other than 1,2 and 3

Default Case

When no case is matched, we can create a default case with the default keyword and execute the code block.

For example –

C++ Code

#include<iostream>
using namespace std;

int main() {
  int x = 4;
  switch (x % 2) {
  case 2:
    cout << "remainder is 2";
    break;
  case 3:
    cout << "remainder is 3";
    break;
  case 4:
    cout << "remainder is 4";
    break;
  default:
    cout << "remainder is something else";
    break;
  }
}

Here, the given expression in the switch statement has an expression that produces 0 as the value. No case matches the value produced by the expression so the default case is executed which gives output as “remainder is something else”.

Let’s take an example problem – Given a character, you need to check if it’s a vowel or not and it it is, you need to print “This vowel is: [vowel]”

input: ch=a
Output: This vowel is: a

Code:

If we use our if-else approach, the code would look something as follows

C++ Code

#include<iostream>
using namespace std;

int main() {
  char ch;
  cin >> ch;
  if (ch == 'a' || ch == 'A') {
    cout << "This vowel is: " << ch << "\n";
  } else if (ch == 'e' || ch == 'E') {
    cout << "This vowel is: " << ch << "\n";
  } else if (ch == 'i' || ch == 'I') {
    cout << "This vowel is: " << ch << "\n";
  } else if (ch == 'o' || ch == 'O') {
    cout << "This vowel is: " << ch << "\n";
  } else if (ch == 'u' || ch == 'U') {
    cout << "This vowel is: " << ch << "\n";
  } else cout << "This is not a vowel" << "\n";
  return 0;

}

If we use switch-case statements, the code would look as follows

C++ Code

#include<iostream>
using namespace std;

int main() {
  char ch;
  cin >> ch;
  switch (ch) {
  case 'a':
  case 'A':
  case 'e':
  case 'E':
  case 'i':
  case 'I':
  case 'o':
  case 'O':
  case 'u':
  case 'U':
    cout << "The vowel is: " << ch << "\n";
    break;
  default:
    cout << "This is not a vowel" << "\n";
  }
  return 0;
}

Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article