Switch statement in C++ programming language

 Switch statement in C++ programming language

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 of Switch Statement in C++ programming language



Basic Working of Switch Statement in C++ Programming Language

In Switch statement we usually take an input from user that input might be an integer or a character. When we take an integer as an input then we use integer values as case value like i.e, case 10: { } it means that if the entered number is equal 10 then run case 10 and execute that specific piece of code related to case 10 and at the end of case 10: coding block use the break key word so that after running case 10 then the control of our program will exit from the switch statement and  move to the next line after ending curly bracket of switch statement in our program.


Simple Coding Example For Switch Statement in C++ Programming Language






In the above coding example we declared a Number variable of type int. Then we are  going to place this number variable in the Switch expression area between the round brackets like this 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. 

And if none of the case value matches then the default case will run. So in this way our switch statement program will be executed successfully.

Congratulations like always we have successfully implemented the Switch statement program.






Comments

Popular posts from this blog

How to run c project in visual studio community 2022

How To Write Hello World Program In C++ Programming Language

if and if-else statement in C programming language