Switch Statement In C++

When we want to test multiple condition same time then we can use a switch statement.The switch statement causes control to be transferred to one of several statements depending on the value of a condition.

The keyword switch is followed by a parenthesized condition and a block, which may contain case labels and an optional default label.

When the switch statement is executed, control will be transferred either to a case label
with a value matching that of the condition, if any, or to the default label, if any.The condition must be an expression or a declaration, which has either integer

or enumeration type, or a class type with a conversion function to integer or enumeration type.case labels can be in any order and must be constants. The default label can be put
anywhere in the switch.

When C++ sees a switch statement, it evaluates the expression and then looks for a matching case label.

If none is found, the default label is used. If no default is found, the statement does nothing.A break statement inside a switch tells the computer to continue the execution after the switch. If the break is not there, execution continues with the next statement.

Syntax of switch statement

switch(value)
{
case 1:
Statement;
case 2:
Statement;
. . . .Default: ///optional here
Statement;
}

Simple Example Of Switch Statement In C++

Create a program in c to print day of week using switch case

#include<iostream>
using namespace std;
int main(){
int choice;
clrscr();
cout<<“Enter a week Number 1 To 7”<
cin>>choice
switch(choice)
{
case 1:
cout<<“Monday”;
Break;
case 2:
cout<<“Tuesday”;
break;
case 3:
cout<<“Wednesday”;
break;
case 4:
cout<<“Thursday”;
break;
case 5:
cout<<“Friday”
break;
case 6:
cout<<“Saturday”;
break;
case 7:
cout<<“Sunday”;
break;
default:
cout<<“Invalid Input Number”;
}
return 0;
}

Output:
Enter a week Number   1   To   7
2
Tuesday

We can also use switch statement with Nested switch.

Simple Example Of Nested Switch Statement In C++

#include <iostream>
using namespace std;
int main(){
int choice;
cout<<“Press 1 for Sum of two Numbern”;
cout<<“Press 2 for Muliplication of two Numbern”;
cout<<“Press 3 for Substraction of two Numbern”;
cout<<“Press 4 for Division of two Numbern”;
cin>>“%d”,&choice
cout<<“Enter two Number”;
cin>>“%d%d”,&a,&b;
switch(choice)
{
case 1:
cout<<“The sum of two Numebr :%d”,a+b;
Break;

case 2:
cout<<“The Multiplication of two Numebr :%d”,a*b;
break;
case 1:
cout<<“The Substraction of two Numebr :%d”,a-b;
break;
case 4:
{
cout<<“Press 1 for quotient of two Number”;
cout<<“Press 2 for Remainder of two Number”;
scanf(“%d”,&choice);
switch(choice)
{
case 1:
cout<<“The quotient of two Number (a/b) is :%d”,a/b;
break;
case 2:
cout<<“The Remainder of two Number is (a%b) :%d”,a%b;
break;
}
break;
}
default:
cout<<“Invalid Input!”;
}
return0;
}

Output:
Press 1 for Sum of two Number
Press 2 for Sum of two Number
Press 3 for Sum of two Number
Press 4 for Sum of two Number
2

Enter two Number
12
2
The Multiplication of two Numebr :24

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Switch Statement In Cpp

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

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password