what is the switch Statement in C++?

Introduction to switch Statement

what is the switch Statement in C++?. In programming, whenever you have to select amongst the multiple options for the same variable switch statement is preferable. It is also possible to implement the decision tree with multiple (nested) if-else structure but there are more chances of wrong implementation with it.

Switch statement C++ Syntax

The general syntax for implementing the switch statement is as follows:

switch( variable)

{

case 1:

break

case 2:

break

.

.

default :

}

After writing the reserved keyword “switch” the variable which can obtain multiple values, is written under different cases. Each case is followed by the constant value that a switch variable can take. That’s why the data type of switch variable and case constants should be matched always.  The break statement is used after each case for terminating the switch. Otherwise the statements after matching with case keep executing until a break statement encountered in the program. what is the switch Statement in C++? I hope the answer is given.

Before entering the switch, it is necessary to assign value to the switch variable that should match to one of the cases mentioned in switch definition.  If the value of switch variable does not match with any of the case constants, then the default case is executed.

Flow chart of switch statement:

flow chart of switch statement
flow chart of switch statement

 

switch statement in C++ example :

int main()

{

clrscr();

int speed;

cout<<“enter speed 33, 44,55”;

cin>>speed;

switch(speed)

{

case 33:

cout<<“the speed is 33”;

break;

case 44:

cout<<“the speed is 44”;

break;

case 55:

cout<<“the speed is 55”;

break;

default:

cout<<“speed is out of range”;

}

in the above example, the speed variable is supposed to take the values from 33,44, or 55. If the value is other than these it is said to be out of range that has been mentioned in default section. The default case can also be skipped in switch statement.

void report()

{
cout<<“it is a very pleasant day today. All staff has received their salary”<<endl;

}
void comfort()

{
cout<<“your employee think you are the best boss”<<endl;

}

void main()

{

clrscr();

int option;

cin>>option;

while(option!=5)

{

switch(option)

{

case 1: cout<<“welcome to menu”<<endl;

break;

case 2: report();

break;

case 3: cout<<“line is busy”<<endl;

break;

case 4: comfort();

break;

default: cout<<“good bye”<<endl;

}

}

getch();

}

Comparison of switch statement and if-else: Switch Vs If-else

Now the question arises where to use switch statement and where to use the nested if-else structure. Usually it depends on the given problem. If the multiple options are available for a single variable then switch statement is used. While in case of multiple conditions for different variables, nested if-else structure is used. For example in the above example the “speed” variables can take different values like 33,44,55 so switch statement was selected. But lets consider a the following case

if( pressure*5>100)

cout<<“greater vale observed”;

else

if( pressure+100<0)

cout<<“not possible”;

else

if(temperature-100>25)

cout<<“mark it”;

It can be noticed different mathematical expressions are used in the nested if-else structure that is not possible to implement using switch structure. Also, here different variables are involved which is not possible with the switch structure. Concluding to our discussion, switch statement always works on the single variable while nested if-else structure can take different variables and expressions.

Example of switch statement

include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int dow;

cout<<“Enter the number of week’s day”;

cin>>dow;

switch(dow)

{

case 1 :

cout<<“\n Sunday”;

break;

case 2 : cout<<“\n Monday”;

break;

case 3 : cout<<“\n Tuesday”;

break;

case 4 : cout<<“\n Wednesday”;

break;

case 5 : cout<<“\n Thursday”;

break;

case 6 : cout<<“\n Friday”;

break;

case 7 : cout<<“\n Saturday”;

break;

default :cout<<“Wrong number of day”

break;

}

getch();

}

Switch statement C++ char Example

#include<iostream.h>

#include<conio.h>

void main()

{ int ch;

float a,b,result;

clrscr();

Cin>>a>>b;

cout<<“Enter any number (1 to 7)”;

cin>>ch;

switch(ch)

{

case ‘+’: cout<<result=a+b;

break;

case ‘-’: cout<<result=a-b;

break;

case ‘*’: cout<<result=a*b;

break;

case ‘\’: cout<<result=a/b;;

break;

default: cout<<“invalid”; }

getch(); }

Output

Enter any number (1 to 7):

5

Today is Friday

Some points to remember in switch statement

As we know that we need to end each case with the break statement. If we do not write break after any case then it will not cause any compilation error. But it will cause some logical error in some scenarios. If one of the case is true and you forget to write break at its end then by default the next case will also be executed until it meets with break. So, the role of break statement is very important in switch statement as it helps to end the case.

This is the means by which it works:

The switch articulation is assessed once

The estimation of the articulation is contrasted and the estimations of each case On the off chance that there is a match, the related square of code is executed The break and default watchwords are discretionary, and will be depicted later in this part.

syntax of switch statement
syntax of switch statement

The default keyword in switch statement

Default gives the switch development an approach to make a move if the estimation of the circle variable doesn’t coordinate any of the case constants. Here we use it to print Try again if the client types an obscure character. No break is vital after default, since we’re toward the finish of the switch in any case. A switch statement is a typical way to deal with examining input entered by the client. Every one of the conceivable characters is addressed by a case. It’s a smart thought to utilize a default statement in all switch statements, regardless of whether you don’t think you need it. A

development, for example,

default:

cout << “Mistake: inaccurate contribution to switch”; break;

alarms the software engineer (or the client) that something has turned out badly in the activity of the program. In the interest of quickness we don’t generally incorporate such a default statement, however you ought to, particularly in genuine programs.

Also read here:

https://eevibes.com/computing/object-oriented-programming/decision-making-statements-in-c/

What are the decision making statements in C++?

Leave a Reply

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