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(EXPRESSION)
{
   case CONDITION1:
     CASE1_CODE;
     break;

   case CONDITION2:
     CASE1_CODE;
     break;
...

   default:
      DEFAULT_CODE;
}
  • `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
#include <stdio.h>

int main () {

   /* Variable which will be used inside the switch case */
   char mygrade = 'B';

   switch(mygrade) {
      case 'A' :
         printf("Your grade is A\n" );
         break;

      case 'B' :
         printf("Your grade is B\n" );
         break;

      case 'C' :
         printf("Your grade is C\n" );
         break;

      case 'D' :
         printf("Your grade is D\n" );
         break;

      case 'E' :
         printf("Your grade is E\n" );
         break;

      case 'F' :
         printf("Your grade is F\n" );
         break;

      default :
         printf("Invalid grade\n" );
   }


   return 0;
}

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  Python If .. Elif .. Else Statements and Conditionals

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.

#include <stdio.h>

int main () {

   /* Variable which will be used inside the switch case */
   char mygrade = 'Z';

   switch(mygrade) {
      case 'A' :
         printf("Your grade is A\n" );
         break;

      case 'B' :
         printf("Your grade is B\n" );
         break;

      case 'C' :
         printf("Your grade is C\n" );
         break;

      case 'D' :
         printf("Your grade is D\n" );
         break;

      case 'E' :
         printf("Your grade is E\n" );
         break;

      case 'F' :
         printf("Your grade is F\n" );
         break;

      default :
         printf("Invalid grade\n" );
   }


   return 0;
}

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 Comment