Switch Statement in C++ with examples

1

The switch statement in C++ is a conditional structure. This statement is a really good alternative of nested if-else. It is used in a situation when there are several cases and we have to execute only one of them. It also uses to execute different conditions according to the input given by the user. This statement is very useful in a case when a programmer has to provide a choice to a user.

Syntax of a Switch Structure:-

Switch (variable)

{

Case A:

{

}

Case B:

{

}

Case C:

{

}

};

Working of a Switch statement:-

C++ Switch statement compare the results of the expressions given by the user with defined multiple cases, expressions could be integer or characters. First of all the expression is evaluated at the beginning of a switch statement then it is compared with different defined cases, if it matches with any of a case then the corresponding blocks of statements will be executed. If it failed to match with all the defined cases then the default box of statements will be executed. Just keep in mind one thing, always use a break statement in the block of statements of every case because break statement is used to exit from the switch structure, when the expression match with the case the corresponding block of statements executes, if the break statement does not present in the body then all the blocks of the remaining cases will also executed.

Consider the following example which will completely describe the working and operations of a switch statement in C++.

Example of a Switch statement:-

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

main()
{

int num;
cout<<"Enter the Number of a Month: " ;
cin>>num;
cout<<endl;

switch (num)
{
case 1:
{
cout << "This is the month of January " ;
break ;
}
case 2:
{
cout << "This is the month of February " ;
break ;
}
case 3:
{
cout << "This is the month of March " ;
break ;
}
case 4:
{
cout << "This is the month of April " ;
break ;
}
case 5:
{
cout << "This is the month of May" ;
break ;
}
case 6:
{
cout << "This is the month of June " ;
break ;
}
case 7:
{
cout << "This is the month of July " ;
break ;
}
case 8:
{
cout << "This is the month of August" ;
break ;
}
case 9:
{
cout << "This is the month of September" ;
break ;
}
case 10:
{
cout << "This is the month of October " ;
break ;
}
case 11:
{
cout << "This is the month of November " ;
break ;
}
case 12:
{
cout << "This is the month of December" ;
break ;
}
default:
{
cout << "Your entered number is invalid" ;
break ;
}
}
getch();
}

Explanation:-Switch statement example The above program will take the number of a month as an input from the user, that input will be stored in a variable num. First it will passed to switch statement in C++, it will be evaluated then it will be compared with the defined cases, if it matched with any case then the block of statements of the respective cases will be executed. If expression does not match with any of the caswitch statement C++ examplese then default case will be executed. Let’s take a look towards other example, which uses characters as an expression to regulate the switch structure. For example:-

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

main()
{

char grade;
cout<<"Enter the Grade of your child: " ;
cin>>grade;
cout<<endl;

switch (grade)
{
case 'A':
{
cout << "Your child is Excellent in class " ;
break ;
}
case 'B' :
{
cout << "Your child is Very Good in class " ;
break ;
}
case 'C' :
{
cout << "Your child is Good in class " ;
break ;
}
case 'D' :
{
cout << "Your child is Poor in class ";
break ;
}
case 'F' :
{
cout << "Your child is Fail in class ";
break ;
}
}

getch();
}

Explanation:-

C++ switch statement

The above program will take Grade of a student as an input from the user and then display the output on the basis of given input. The input will be stored in a variable Grade. Then passed to C++ switch statement, it will check the Variable stored in a grade and According to the condition it will execute the case A, case B, case C or case D. One thing should be kept in mind while using character as an expression, always put apostrophe around the character during declaring a case otherwise it will generate error.switch statement example

[yop_poll id=”10″]

 

If you like my article on switch statement in C++ and wanted to read my other articles then visit my Homepage.

@ 2014 HellGeeks.com

5/5 - (1 vote)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a reply