SWITCH-CASE in C++

Switch Case in C++
Switch Case in C++

The error comes when you do this:

Forgetting a break statement when one is needed in a switch structure is a logic error

Providing identical case labels in a switch structure is a syntax error

Omitting the space between the word case and the integral value being tested in the switch structure can cause a logic error. For example, writing case3 : instead of writing case 3: simply creates an unused label.In this situation, the switch structure will not perform the appropriate actions when the switch’s controlling expression has a value of 3.

The switch structure consists of a series of case labels and an optional default case.

In the switch case, each case can have one or more actions. The switch selection structure is different from all other control structures in that braces are not required around multiple actions in the case of a switch. A majority of switch structures use a break in each case to terminate the switch structure after processing the case. without the break statement, control would not transition to the end of the switch structure after a case is processed. Instead, control would transition to the next case’s actions

The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.
syntax of switch:

Note* In a switch structure that list the default clause last, the default clause does not require a break statement.Some programmers include this break for clarity and for symmetry with other cases.

Note* Provide a default case in the switch statements. Case not tested explicitly in a switch statement without a default case is ignored. Including a default, the case focuses the programmer on the need to process exceptional conditions. There are situations in which no default processing is needed. Although the case clauses and the default case clause in a switch structure can occur in any order, it is a good programming practice to place the default clause last

switch(expression/variable)
{
case 1:statement;
break;
case 2:statement;
break;
case 3:statement;
break;
case 4:statement;
break;
-
-
default:statements;
} 

The switch statement is different from similar statements in languages such as Pascal in a very important way. Each C++ case label functions only as a line label, not as a boundary between choices. That is after a program jumps to a particular line in a switch, it then sequentially executes all the statements following that line in the switch unless you explicitly direct it otherwise. Execution does not automatically stop at the next case. To make an execution stop at the end of a particular group of statements, you must use the break statement. This causes execution to jump to the statement following the switch

1.Example for Switch case in C++

include<iostream> 
 using namespace std; 
 int main () { 
 int num; 
 cout<<"Enter a number to check grade:";  cin>>num; 
 switch (num) 
 { 
 case 10: cout<<"It is 10"; break; 
 case 20: cout<<"It is 20"; break; 
 case 30: cout<<"It is 30"; break; 
 default: cout<<"Not 10, 20 or 30"; break; 
 } 
 } 

OUTPUT:

Enter a number:
10
It is 10
Output:
Enter a number:
55
Not 10, 20 or 30

2.Simple calculator using switch case in C++

#include<iostream>  
using namespace std;
int main() 
 {
  char o; 
 float num1, num2;
cout << "Enter an operator (+, -, , /): ";  cin >> o;  cout << "Enter two operands: ";  cin >> num1 >> num2;  switch (o)  {  case '+': cout << num1 << " + " << num2 << " = " << num1+num2;  break;  case '-': cout << num1 << " - " << num2 << " = " << num1-num2;  break;  case '': cout << num1 << " * " << num2 << " = " << num1*num2;
  break;
  case '/': cout << num1 << " / " << num2 << " = " << num1/num2;
  break;
  default: // operator is doesn't match any case constant (+, -, *, /) cout << "Error! operator is not correct"; 
 break;
  } 
 return 0; 
 }

OUTPUT

Enter an operator (+, -, *, /): +
Enter two operands: 5
2
5 + 2 = 7  

3.Display the day Name according to the day number(1-7) in C++

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<"\n Enter number to convert day (1 to 7):";
cin>>n;
switch(n)
{
case 1:cout<<"\n Monday";
break;
case 2:cout<<"\n Tuesday";
break;
case 3:cout<<"\n Wednesday";
break;
case 4:cout<<"\n Thursday";
break;
case 5:cout<<"\n Friday";
break;
case 6:cout<<"\n Saturday";
break;
case 7:cout<<"\n Sunday";
break;
default:
cout<<"\n Wrong Number";
break;
}
getch();
}

OUTPUT:

Enter Number to convert day (1 to 7):4
Thursday

4. Program to check whether the given character is Vowel or not in C++

#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
cout<<"Enter the character:";
cin>>ch;
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
cout<< "It is a Vowel";
break;
default:
cout<< "It is not a Vowel";
}
getch();
}

OUTPUT:

Enter the character :O
It is a Vowel

The nested switch statement in C++

In C++ nested switch statements means when any switch statement is defined inside the switch statement it is known as nested switch statement in C++

Example for nested switch statement:

#include<iostream>
#include<conio.h>
void main()
{
clrscr();
int a=100;
int b=200;
switch(a)
{
case 100:
cout<<"In Outer Switch"<<endl;
switch(b)
{
case 200:
cout<< "In Inner Switch"<< endl; 
}
}
cout<<"Value of a is:"<<a<<endl;
cout<<"Value of b is:"<<b<<endl;
getch();
}

OUTPUT:

In Outer Switch.
In Inner Switch.
Value of a is : 100
Value of b is : 200
Translate »