Switch Case Statement in C/C++ Tutorial with Examples – POFTUT

Switch Case Statement in C/C++ Tutorial with Examples

C/C++ programming languages provides switch ... case statement for selection according to given switch state. switch  ... case can be used for different cases in order to control the flow of the application.

Syntax

switch ... case has the following syntax.

  • switch is the keyword used to create a switch ... case structure.
  • EXPRESSION is the which will be checked against provided cases.
  • case keyword is used to create a new case with the specified condition.
  • CONDITION specifies the conditions related to the case.
  • CASE_CODE is used to execute if the given condition is met for the given variable.
  • break is used to end the given case and exit from it. Normally after the matched case, the below cases will be tried to match but the break will end the complete switch block and do not check the following cases.
  • default case is used when no one of the previously defined cases are met.
  • DEFAULT_CODE is used to run when the default case is executed.

Switch … Case Statement Example

Well, the switch case statement is a bit hard to understand according to the other keywords and mechanisms because it contains a lot of code.

Switch ... Case Statement Example

Switch … Case Statement Example

In this example, we will set the variable mygrade as B and this will match with the case B and print to the screen Your grade is B.

Switch Statement Rules

While using switch ... case statement there are some rules to obey.

  • The expression should be a result of constant value.
  • Same value cannot be used for multiple cases.
  • The default statement is optional.
  • break statement is optional but in generall it is used in most cases in order to stop the current check flow of the switch case.
  • Multiple switch case blocks can be nested but it should be avoided because it will make the application hard to read and understand.
LEARN MORE  Powershell If-Elseif-Else Conditional Statement

Default Statement

default statement is used to run code if there is no match in the existing cases. This can be very helpful to run code in unspecified cases. We will add the default ad the end of the cases and do not provide any case and just provide the default code block we want to run. In the following example we will provide mygrade as Z so it will match the default case and print screen Invalid grade.

Break Statement

The normal behavior of the switch case is flowing from top to down and run matches cases code block and continue for the following cases. But this is generally unwanted situations where after a case is matched and code block executed we generally prefer to exit from switch case. We can use break statement after executing a case code block which will end the switch case completely and does not check for the following cases.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)