In previous article, we have mentioned C++ file handling, now in this article, we will be understanding about switch case statement in C++ and how to use it with an example.

Switch case Syntax in C++

Before we begin looking for switch case c++ example, let's check the Syntax of Switch case in C++

switch(expression) {
     case x:
       // code block
     case y:
       // code block
      // code block

as you can see in the above syntax, we have switch keyword with expression.

When the switch statement is evaluated, it compares the expression’s value with every single case, for example, cases x and y as mentioned above.

If expression = x, then code block for x will be executed, and "break" keyword, will make compiler to exit from switch case, so other case statement doesn't get executed.

if expression = y, then code block for y will be excuted.

If none of the case matches the expression value, default code block will be executed.

For example, in the below code

switch(1) {
    case 1 : cout << '1'; // prints "1",
    case 2 : cout << '2'; // then prints "2"

Output will be '1' and then '2', since expression 1 = case 1, but there is no "break" keyword, so case2 will also be executed.

Take a look at another example, with break keyword

switch("a") {
    case "a" : cout << 'a'; // prints "a"
             break;       // and exits the switch
    case "b" : cout << 'b';

In the above code, output = "a", since, expression = a and once code in "case a" is executed, there is "break" statement, which will help compiler exit the switch case.

Switch case C++ Example

Let's take a look at complete example of switch case statement in C++.

We are using Visual Studio for this example, so I have created a new C++ project in Visual Studio, by navigating to File -> New -> Project -> Select "Visual C++" from left pane and Console application from right, name it and click "Ok", as shown below


Now, navigate to "Source Files" -> "SwitchCaseCpp.cpp" and use the below code

#include "pch.h"
#include <iostream>

void digitEntered(int x)
	switch (x)
	case 1:
		std::cout << "1 entered \n";
	case 2:
		std::cout << "2 entered \n";
	case 3:
		std::cout << "3 entered \n";
		std::cout << "Unknown \n";

int main()
	digitEntered(2); //calling for case 2

	digitEntered(5); // passing 5 as case expression, since case doesn't exists, 'default' case will be executed

	std::cin.clear();  // clear buffer
	return 0;

Once you will execute the above code, you will see output as below


In the above code, we are calling function "digitEntered(2)", when function is called with parameter "2", then switch case expression = 2, so case 2: is executed, and since we are using "break", compiler exit from the code.

Then, we are calling "digitEntered(5)" with parameter value as "5", since we don't have any case with 5, default switch statement code is executed.

Other two lines

std::cin.clear();  // clear buffer

are used to keep console open, until hit "Enter" twice.

Advantages of Switch case

  • Easier to read than if-else statement
  • Faster and better approach instead of nested if-else.
  • Easy to debug code and maintain code.
  • It has best-optimized implementation for faster code execution than the "if-else if" statement

When we should use switch case statement?

As stated above, switch case statement is faster than nested if-else-if-else...statements, so if you have lots of nested if-else statements, then it is better to use switch case statement.

Since, it will make code execution faster and it is easier to maintain code.

You may also like to read:

C program to print pyramid pattern, explained with code example

Program for Matrix multiplication in C (With & Without pointers)

Binary search program in C with Algorithm