Switch case statement in Cpp language
Switch case statement in Cpp language
In this tutorial, we will discuss the Switch case statement in Cpp language
Switch statements in C++ select one option from multiple options of cases based on a switch expression.
The switch case statement is similar to if else if … else ladder. but it is not as same as if else if…else. The switch case statement is used to execute a block of code from many options
The following syntax of the switch case statement to clearly depicts the flow of switch case statements.
The syntax of Switch case statement
Syntax
switch(expression) { case constant_1: //block of statements 1 //code to be executed, if the expression is matched to constant_1(case 1) break; case constant_2: //block of statements 2 //code to be executed, if the expression is matched to constant_2(case 2) break; case constant_3: //block of statements 3 //code to be executed, if the expression is matched to constant_3(case 3) break; default: default block; //code to be executed, if the expression doesn't matched to any constant_1(case 1) break; }
In the switch case statement, when a case statement is matched with the switch expression, the flow of control of the program will pass the block of code associated with case statements
Switch Statement Flowchart
C program switch case statement
- First, the expression of the switch statement is evaluated only once.
- Then the value of the expression is compared with the values of each case
- When it found matched expression, the associated block of code is executed
- The break and default statements are optional, the break is used to end the flow of control for the associated case.
- the default is executed if any case doesn’t match with switch expression
Example program of Switch case
Example of Switch case statement in Cpp language
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int age=30; switch(age){ case 20: cout<<"age is equal to 20"; break; case 22: cout<<"age is equal to 22"; break; case 24: cout<<"age is equal to 24"; break; case 26: cout<<"age is equal to 26"; break; default: cout<<"age is not equal to any value"; } getch(); return 0; }
When the above code executed, it produces the following results
age is not equal to any value
In the above program, if the age is defined as 30. the flow of control checks every case to find an alternative case. Then the flow of control fails to find the associated case . the default statement will be displayed as follows
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int month; cout<<"Enter the month: "; cin>>month; switch(month){ case 1: cout<<"This month is January"; break; case 2: cout<<"This month is February"; break; case 3: cout<<"This month is march"; break; case 4: cout<<"This month is april"; break; case 5: cout<<"This month is May"; break; case 6: cout<<"This month is June"; break; default: cout<<"your month is not available in first half months"; break; } getch(); return 0; }
When the above code executed, it produces the following results
Enter the month: 4 Tismonth is april
Points to remember of the switch case statements
- The switch statement may contain one or many numbers of cases
- Duplication of case value is not allowed it must be unique
- The value of the case must be the same data type of the variable of the switch
- The expression of a switch statement must be like a byte, short, int, long, enums etc..
- Break statements are used to terminate the sequence of statements when executed
- The break statement is optional but if we skip the break statements and it will not terminate execution and the control will moves to the next case from the executed case
- The default statements are optional when any case is not supported with switch statements the default statements will be displayed.
Break statements in Switch case
The break statement is optional in switch case statement but when we use break statements of switch case statements, we can get clear output with the associated case.
let’s see the example below where I am not using the break statements
#include <iostream> #include <conio.h> using namespace std; int main() { int num=3;; switch(num){ case 1: cout<<"sunday\n"; case 2: cout<<"Monday\n"; case 3: cout<<"Tuesday\n";//associated case case 4: cout<<"Thursday\n"; case 5: cout<<"Friday\n"; case 6: cout<<"Saturday\n"; Default: cout<<"No match any cases"; } getch(); return 0; }
When the above code executed, it produces the following results
Tuesday Thursday Friday Saturday No match any cases
Suggested for you