Switch statement in C++ programming language
Switch statement in C++ programming language
In todays blog post we are going to cover a very important decision making statement which is used to execute or run different blocks of code based on the value of the expression. Always remember that the switch statement is used as an alternate to the if else if ladder statement in C++ programming language. The switch statement is more cleaner and is considered to be more efficient as compared to the if else if ladder statement. And just keep this thing in mind that, the expression value that is used with in the switch statement it must be an integer value, or a character value, or enumeration type.
In programming languages we have studied about conditional statements in which accross given set of conditions we chose any specific condition and run the block of code as per that condition. There are other techniques that we call control statement or decision making statements. The Switch statement is one of the very important example of such control statement or deciison making statements. The switch is used to to select and execute one block of code from several possible options. The Switch statement is alternatively used in place of a long if-else-if ladder statement which is an example of conditional statement. It is more cleaner as compared to if-else-if ladder and also it is more efficient for the compiler to process it.
Basic Syntax and working of Switch Statement in C++ programming language
Syntax of switch statement
switch (expression) { case value1: { // lines of code to be executed here. break; } case value2: { // lines of code to be executed here. break; } case value3: { // lines of code to be executed here. break; } default: { // lines of code to be executed here. break; } }
Basic working of switch statement in C++ programming language
Now let us look at the basic syntax of switch statement in C++ programming language. As you can see that in the above coding syntax we have used the switch keyword followed by the opening and closing round brackets and with in these round brackets we can simply write some piece of expression. Now remember that this expression value can be either an integer or a character. And on the basis of that expression value we can make different cases in switch statement as per the logic of our C++ program. And always remember that at the end of each case we have to to write a break statement. And there is another very important point to keep in mind that duplicate case values are not allowed in switch statement in C++ programming language. This break statement will simply terminate the switch statement and the control of our program will move out of the body of switch statement. And if the none of the switch case values match to the expression value then at last the default case will run and the code with in the body of default case will be run automatically.
Simple Coding Example of Switch Statement in C++ Programming Language
#include<iostream> #include<conio.h> using namespace std; void main() { int Number; cout << "Enter Number = "; cin >> Number; switch (Number) { case 10: { cout << "Entered Number is " << Number; break; } case 20: { cout << "Entered Number is " << Number; break; } case 30: { cout << "Entered Number is " << Number; break; } default: { cout << "Entered Number is not 10, 20, or 30"; break; } } _getch(); }
Explanation of above C++ coding example
In the above coding example we declared a Number variable of type int.
int Number; cout << "Enter Number = "; cin >> Number;
Then we are going to place this number variable in the Switch expression area between the round brackets like this switch (Number).
switch (Number)Now in this coding example we are using 10, 20, or 30 as our case values which means that if user will enter value 10 then the case 10: will run and the code written inside case 10: block will run and in the same way if user enters 20, or 30 then the specific cases will run and each case has a break keyword to exit from the switch statement.
case 10: { cout << "Entered Number is " << Number; break; } case 20: { cout << "Entered Number is " << Number; break; } case 30: { cout << "Entered Number is " << Number; break; }
And if none of the case value matches then the default case will run.
default: { cout << "Entered Number is not 10, 20, or 30"; break; }
So in this way our switch statement program will be executed successfully.
Congratulations! like always we have successfully implemented the Switch statement in C++ programming language.
Comments
Post a Comment