C++ Switch Statement

While we have several circumstances and will need to take different actions depending on the situation, we utilize the switch case declaration. When there are several criteria, we will need to run several instructions when they are met. In this instance, we may use a long if..else-if declaration or a switch statement. If we have multiple criteria, the difficulty with long if…else-if is that it will become complicated. The switch case is indeed a neat and effective way to treat such states. A switch statement enables a mutable just to be compared to a set of values for equivalence. Every value is referred to as a situation, so each situation is verified against the mutable that is already being turned on. A switch statement is subject to the subsequent guidelines:

  • The phrase should be of any integer or enumerate form or a c type with a sole conversion method to an integer or enumeration form inside a switch declaration.
  • A switch can contain any sum of case declarations. Every case is trailed by a colon and the value to be matched to.
  • A case’s constant phrase should be of the identical type of data as that of the switch’s mutable, and it has to be either a constant or a number.
  • Whenever the mutable being turned on equals a case, the expressions that precede it could run unless a break declaration is encountered.
  • The switch finishes whenever a break declaration is encountered, and the stream of control fences to the streak after the switch declaration.
  • A break isn’t required in every instance. If no break emerges, the control stream will continue through future instances unless a break is found.
  • A default choice can be specified in a switch declaration and must come after the switch. While not any of the instances are true, the default scenario could be utilized to complete a job. Throughout the default scenario, no break is required.

Example 01:

Let’s get started with our first example to see the working of a Switch statement in C++. At the time of implementing this guide, we have been using Ubuntu 20.04. Open the command console terminal on Ubuntu 20.04 via the shortcut key “Ctrl+Alt+T”. Now the terminal has been launched, create a new c++ file named “test.cc” using the touch query as below.

Now the file has been created, you can check it in your Home folder. To open this file for edit, use the nano editor to do so. Hence, use the nano command to open it as below.

Now the file has been properly opened in the Nano editor. You have to write the below code in your file as it is. This code contains the input-output stream header at the top of the file. We have to use the namespace to avoid any circumstances in the c++ language. The main function has been initialized to use the switch statement in it. We have initialized a variable “num” with the value “2” in it. After that, we have started the switch statement while parsing the “num” variable in it. Now the cases of switch statements are being started. There is a total of three cases we have been using here. All three cases are integer cases. If any of the cases match with the value of variable “num”, the print statement of that particular case will be executed without delay. All the other cases will be ignored while the case is met. The break statement has been used to break the flow of the switch statement while the case is met, and the control will be out of the switch statement. The main method closes once the switch ends. Press “Ctrl+S” to save the C++ code in your system. Quit the Nano editor using “Ctrl+X”.

Let’s first compile our c++ code to make this file executable. For this purpose, we must have a C++ compiler installed and configured on our Linux operating system. If nor try to use the below query in your shell to do so quickly.

$ sudo apt install g++

Now the compiler has been installed, write the below command and press Enter to compile.

There are no errors found while compilation of the test.cc file. Let’s move to the execution of a file using the below query. The result shows the output “Two” in the terminal as case 2 has been met with the variable “num” value 2.

Example 02:

Let’s have another example to see the working of the switch statement here. Copy the code below in your file. This time we have been using string type variable “color” having value “blue” to be passed in the switch statement. We have used the two cases, “w” for white and “b’ for black, in the switch. A new thing has been added to this code, e.g., the default case. This case will be executed if the value passed to the switch statement doesn’t meet any of the cases mentioned in it, as you can see that the color is “Blue” and doesn’t match both the cases. This time default case must be executed. Save your code and quit it.

Compile the c++ code via g++ compiler as below, followed by the name of a file.

The execution of a file will be taken place using the output command stated below. The output shows that the print statement from the default case has been executed as below.

Example 03:

Let’s take another example similar to the above. Open the test.cc file via the nano editor.

Write the below code in your opened file as it is. In the main method, we have initialized an integer “n”. We have been asking a user to add the marks of a student using the “cout” phrase. The statement “cin” has been used to store the entered value by the user to the variable “n”. The user-added value will be used in the switch statement to check and execute the relative case that matches the user-added value.

Compile the code first.

Upon execution, the user has entered 40 and the case “40” executed.

Upon executing the file again, use added the value 10, which doesn’t match any case. Hence, the default case has been executed.

Example 04:

This time we have been using a switch statement to work as a calculator. So, we have added the character type variable “op”, two float type variables n1 and n2. According to the operator added by the user, the switch statement will execute the particular related case. The relative case will calculate the value by applying the particular operator on both the operands.

Compile the code.

The user added the “/” sign and two numbers upon first execution, and the division has been performed.

The user added the “*” sign and two numbers upon second execution and performed multiplication.

The user added the “&” sign and two numbers upon third execution and performed the default case.


This article has successfully discussed the switch statement, its cases, and default cases in the examples. We hope it will help you cater to all the issues regarding switch statements in the C++ language.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.