Switch Statement in C++

Hello learners, in the previous lecture, we’d discussed about the if-else statement in C++. Now, if you don’t know about the if-else statement, then, it is highly recommended that you should read the if-else lecture, because if-else are the basic building blocks of any programming language. In this article, we’ll discuss the switch statement in C++.

Note: If you already know programming & just want to practice, then click here.

Introduction

Here’s the official documentation of the switch statement. We highly recommend you to go through the official documentation.

Switch statement is in place of if-else-if statement as discussed in the previous lecture. In switch statement, we have to choose one of the many different code blocks based on the case. Now, let’s jump straight in the switch statement.

Syntax of Switch Statement

Below is the syntax of switch statement:

switch(expression){
	Case x:
		// block of code
	Case y:
		// block of code
	Default:
		// block of code,
}

Now, let’s understand the working of the above code in detail.

switch statement allows us to test the variable equally with other values. Each value is called a case.

The code inside the case x or case y will execute only if ‘x’ or ‘y’ matches the ‘expression’. If none of the expression matches, then code inside ‘default’ executes.

We can think of default, as the ‘else’ statement, inside which, the code always executes.

Often times, we write break in the code of every case, though, it is optional. We’ll see it later.

Example of Switch Statement

#include <iostream>
using namespace std;
int main(){
	int number = 7;
	switch(number){
		case 1:
			cout<<”The number is: ”<<number<<endl;
		case 2:
			cout<<”The number is: ”<<number<<endl;
		default:
			cout<<”The number is: ”<<number<<endl;
        }
        return 0;
}

Output

The number is: 7

In the above example, we’ve seen that the variable ‘number’ is 7 & it doesn’t matched with any of the case, so the code inside the default executed. It’s your choice if you want to use default or not, your program will run without any error. However, in the above code, if we don’t use default statement, no output will be displayed.

Flow Diagram of Switch Case

  • Save

Break Statement in switch case

Whenever the program counter encounters the break statement inside switch case, then it takes the program counter outside the switch statement. You can learn more about break statement in C++.

Now, let’s understand, what will happen if we don’t use switch statement.

#include <iostream>
using namespace std;
int main(){
	int number = 2;
	switch(number){
		case 2:
			cout<<”The number is: 2” <<endl;
		case 1:
			cout<<”The number is: 1“<<endl;
		case 4:
			cout<<”The number is: 4”<<endl;
		case 3:
			cout<<”The number is: 3”<<endl;
		default:
			cout<<”This is default”<<endl;
        }
        return 0;
}

Output

The number is: 2
This is default

Now, in the above example, we’ve seen that since case 2 was matching the expression inside switch statement, even then it is checking the other switch cases. This is why the code inside the default case is executing.

If we use break statement inside the switch case, then, after encountering the break statement inside the switch case, the program counter will automatically come out of the switch case, thus, it will not come across the default case. This is the reason of using break statement inside the switch case.

Now, let’s see the same example with ‘break’ statement including.

#include <iostream>
using namespace std;
int main(){
	int number = 2;
	switch(number){
		case 2:
			cout<<”The number is: 2” <<endl;
			break;
		case 1:
			cout<<”The number is: 1“<<endl;
			break;
		case 4:
			cout<<”The number is: 4”<<endl;
			break;
		case 3:
			cout<<”The number is: 3”<<endl;
			break;
		default:
			cout<<”This is default”<<endl;
        }
        return 0;
}

Output

The number is: 2

Now, only the code inside case 2 got executed. Rest of the cases including default, got ignored.

So, this is all about the switch statement. In every language, the syntax may change, but the concept mostly remains same. So, just focus on the concept & try to practice as much as you can.

If you have any queries about this lecture or if you find any problem in the above article, do mention in the comments. If you like the article, don’t forget to share it.

In the next tutorial, we’ll discuss for loop. Till then stay tuned.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share via
Copy link
Powered by Social Snap