AppDividend
Latest Code Tutorials

Switch case in C++ Program | C++ Switch Example

0

Switch case in C++ Program | C++ Switch Example is today’s topic. Switch case is prolonged if-else conditions like we have many conditions, and we need to perform different actions based on that condition. Switch case statements are used if we want a particular block of code to run only if a specific condition is satisfied. We have already seen the For loop and while loop in C++.

Switch case in C++

Switch case is used at the place of lengthy if-else. It is because of increased complexity in the case of nested if-else as a lot of parenthesis and conditions deteriorate the neatness of the code and makes it hard to understand.

Syntax of switch case in C++

switch(expression)
{
	case constant:			
			//code block
	case constant:			
			//code block
	case constant:			
			//code block
	default:  
			//code block
}

See the code.

int a=2;
switch(a)
{
  case 1:  	cout<<"Hello I am case one!"<<endl;
		break;
  case 2: 	cout<<"Hello I am the case two!"<<endl;
		break;
  case 3: 	cout<<"Hello I am case three!"<<endl;
		break;
  default: 	cout<<"Hello I am default"<<endl;
}

See the following output.

Hello, I am case two!

Explanation: In the above code, I have passed an in the switch statement. The value of a there is two hence case 2 gets executed over there;

Note:

It is essential to use break statement in all the cases except the default as if we don’t write break in each case statement all the following case gets executed. For example, in the above code, if we don’t write a break in each case statement, we will get the following output.

Hello I am case two!
Hello, I am case three!
Hello, I default!

Hence, writing a break in every case statement is essential as it takes us out of the switch case after the execution of that particular case which is the requirement of the question.

Flowchart of the Switch case

 

Switch case in C++ Program

Note: The variable used in the switch case must be one of the following from short,

byte, int, char. The values of each case must be the same datatype as of the variable.

Essential Points about Switch Case Statements

  1. Duplicate case values are not allowed.
  2. An expression provided in a switch should result in the constant value; otherwise, it would not be valid.
  3. The default statement is optional. Even if the switch case statement does not have the default statement, it would run without any error.
  4. A break statement is used inside a switch to terminate the statement sequence. When the break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  5. The break statement is optional. If omitted, execution will continue into the next case. A flow of control will fall through to subsequent cases until a break is reached.
  6. Nesting of switch statements is allowed, which means you can have the switch statements inside another switch statement. However, the nested switch statements should be avoided as it makes a program more complex and less readable.

Example programs of switch case

Q1- Write a program to demonstrate the mechanism of switch case.

#include<iostream>
using namespace std;
int main(){
	
char a ='c';
switch(a)
{
 case 'a':  	
	cout<<"Case 1"<<endl;
	break;
 case 'b': 	
	cout<<"Case 2"<<endl;
	break;
 case 'c': 	
	cout<<"Case 3"<<endl;
	break;
 default: 	
	cout<<"Default case"<<endl;
 }
 return 0;
}

See the output.

 

Example programs of switch case

Q2- Write a program to do addition, subtraction, multiplication using switch case. 

#include<iostream>
using namespace std;
int main(){
	
char x ='c';
int a=5,b=6,d;
switch(x)
{
  case 'a':  	
		d=a+b;
		cout<<"The value of addition is= "<<d<<endl;
		break;
  case 'b': 	
		d=b-a;
		cout<<"The value of substraction is= "<<d<<endl;
		break;
  case 'c': 
		d=b*a;	
	cout<<"The value of multiplication is= "<<d<<endl;
	break;
  default: 	
	cout<<"Default case"<<endl;
  }
  return 0;
}

See the output.

 

C++ Switch Example

Finally, Switch case in C++ Program | C++ Switch Example is over.

Recommended Posts

C++ Array Tutorial With Example

C++ Lambda Function Tutorial

C++ Iterators Tutorial With Example

C++ List Tutorial With Example

C++ Constructor Tutorial With Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.