C++ Switch Statement Example

In this article iam going to talk about C++ Switch Statement Example, also you can watch the video training for this article at the end . so switch statement is used when we have multiple conditions and we need to perform different action based on the condition. For example if we have different conditions and we want to execute a block of code based on certain condition. In that case we need to use switch statement. Also you can use else if condition, but it is good idea if you have multiple conditions you can use switch statement.

 

 

Also you can check more articles on c++ programming language

1: C++ Introduction & Program Structure 

2: C++ Variables And Data Types 

3: C++ Getting User Input 

4: C++ If Else Statement 

5: C++ Short Hand If Else

 

 

 

The syntax of switch statement

 

 

Some main points for switch statement

  • You can use non-numerical values with the switch statement.
  • Duplicate case values are not allowed.
  • The cases do not have to be in order, as long as you use proper requirements and syntax.
  • The cases cannot have the same value, as this will result in a duplicate value error.
  • You don’t have to use a break statement for each case.If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached.

 

 

 

OK now this is our example and our  example is based on the user input.

 

At the top of this code first we have created a variable of type char. after that we are going to get the input from the user and store that input in the char variable. after that we are checking the cases, after run this will be the result.

 

 

 

 

Also you can watch the complete video for C++ Switch Statement Example

 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×