Switch Case in C++

Advertisements

Prev Tutorial Next Tutorial

Switch Case in C++

The switch statement in C++ language is used to execute the code from multiple conditions or case. It is same like if else-if ladder statement.

A switch statement work with byte, short, char and int primitive data type, it also works with enumerated types and string.

Syntax

switch(expression or variable)
{
case  value:
//statements
// any number of case statements
break;  //optional
default: //optional
//statements
}

Flow chart

Switch Case

Rules for apply switch

  • The switch expression must be of integer or character type.
  • With switch statement use only byte, short, int, char data type.
  • With switch statement not use float data type.
  • You can use any number of case statements within a switch.
  • The case value can be used only inside the switch statement.
  • Value for a case must be same as the variable in switch.

Valid statement for switch

Switch Case in Example C++

int x;
byte y;
short z;
char a,b;

Limitations of switch Case in C++

Logical operators can not be used with switch statement. For instance

Example

case k>20&&k=20:	// is not allowed

Switch case variables can have only int and char data type. So float data type is not allowed.

case 4.2:  // Invalid 

Syntax

switch(ch) 
{ 
case 1:   
statement 1; 
break; 
case 2: 
statement 2; 
break; 
} 

In this ch can be integer or char and can not be float or any other data type.

Rulse table of switch

Valid SwitchInvalid SwitchValid CaseInvalid Case
switch(a)switch(2.3)case 3;case 2.5;
switch(a>b)switch(a+2.5)case 'a';case x;
switch(a+b-2)case 1+2;case x+2;
switch(func(a,b))case 'a'>'b';case 1,2,3;

Example of Switch Case in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int ch;
clrscr();
cout<<"Enter any number (1 to 7)";
cin>>ch;
switch(ch)
{
case  1:
cout<<"Today is Monday";
break;
case  2:
cout<<"Today is Tuesday";
break;
case  3:
cout<<"Today is Wednesday";
break;
case  4:
cout<<"Today is Thursday";
break;
case  5:
cout<<"Today is Friday";
break;
case  6:
cout<<"Today is Saturday";
break;
case  7:
cout<<"Today is Sunday";
break;
default:
cout<<"Only enter value 1 to 7";
}
getch();
}

Output

Enter any number (1 to 7): 5
Today is Friday

Note: In switch statement default is optional but when we use this in switch default is executed at last whenever all cases are not satisfied the condition.

Example of Switch without break in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int ch;
clrscr();
cout<<"Enter any number (1 to 7)";
cin>>ch;
switch(ch)
{
case  1:
cout<<"Today is Monday";
case  2:
cout<<"Today is Tuesday";
case  3:
cout<<"Today is Wednesday";
case  4:
cout<<"Today is Thursday";
case  5:
cout<<"Today is Friday";
case  6:
cout<<"Today is Saturday";
case  7:
cout<<"Today is Sunday";
default:
cout<<"Only enter value 1 to 7";
}
getch();
}

Output

Enter any number (1 to 7): 4
Today is Thursday
Today is Friday
Today is Saturday
Today is Sunday

Note: In switch statement when you not use break keyword after any statement then all the case after matching case will be execute. In above code case 4 is match and after case 4 all case are execute from case-4 to case-7.

C++ program for calculator

Example Calculator

#include<iostream.h>
#include<conio.h>

void main()
{
char choice;
int a,b,res=0;
clrscr();
cout<<"Enter first value: ";
cin>>a;
cout<<"\n Enter operator: ";
choice=getch();
cout<<"\n Enter second value: ";
cin>>b;

switch(choice)
{
case '+':
  res=a+b;
  cout<<"Sum: ",res;
break;
case '-':
  res=a-b;
  cout<<"Difference: ",res;
break;
case '*':
  res=a*b;
  cout<<"Product: ",res;
break;
case '/':
  res=a/b;
  cout<<"Quotient: ",res;
break;
default:
  cout<<"Enter Valid Operator!";
}
getch();
}

Output

Enter First number: 6
Enter operator : +
Enter Second number: 10
Sum: 16

Explanation: In above code choice=getch(); is used for scan or get one value by using getch() function from keyboard, and store is choice.


Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements