Switch Case Statement in C Language

Beginner

Introduction

In C, the switch case statement is used to control the execution flow of a program when there are multiple alternatives available. It is one of the decision-making statements in C. The switch case statement evaluates an expression and matches it with multiple values to execute a corresponding block of code.

In this lab, you will learn how to use the switch case statement in C with examples.

Basic Switch Case Syntax

#include <stdio.h>

int main() {
  int num = 2;
  switch(num) {
    case 1:
      printf("Number is 1");
      break;
    case 2:
      printf("Number is 2");
      break;
    case 3:
      printf("Number is 3");
      break;
    default:
      printf("Number is not 1, 2, or 3");
  }
  return 0;
}

This program declares an integer num with a value of 2 and uses the switch case statement to check if its value matches with any of the case labels. Since the value of num is 2, the output of this code will be Number is 2.

Without Break Statement

#include <stdio.h>

int main() {
  int num = 2;
  switch(num) {
    case 1:
      printf("Number is 1\n");
    case 2:
      printf("Number is 2\n");
    case 3:
      printf("Number is 3\n");
    default:
      printf("Number is not 1, 2, or 3\n");
  }
  return 0;
}

This program declares an integer num with a value of 2 and uses the switch case statement without using the break statement. Since there is no break statement, all the subsequent statements will be executed until the next break statement or the end of the switch case statement. The output of this program will be:

Number is 2
Number is 3
Number is not 1, 2, or 3

Using Char Data Type

#include <stdio.h>

int main() {
  char grade = 'B';
  switch(grade) {
    case 'A':
      printf("Excellent");
      break;
    case 'B':
      printf("Good");
      break;
    case 'C':
      printf("Average");
      break;
    default:
      printf("Invalid grade");
  }
  return 0;
}

This program declares a character variable grade with a value of 'B'. It uses the switch case statement to check if the value of grade matches with any of the case labels. The output of this code will be Good.

Nested Switch Case

#include <stdio.h>

int main() {
  int num1 = 2, num2 = 3;

  switch(num1) {
    case 1:
      switch(num2) {
        case 1:
          printf("Number is 1");
          break;
        case 2:
          printf("Number is 2");
          break;
        default:
          printf("Number is not 1 or 2");
          break;
      }
      break;
    case 2:
      switch(num2) {
        case 3:
          printf("Number is 3");
          break;
        case 4:
          printf("Number is 4");
          break;
        default:
          printf("Number is not 3 or 4");
          break;
      }
      break;
    default:
      printf("Invalid input");
  }
  return 0;
}

This program declares two integer variables num1 and num2 with values of 2 and 3 respectively. It then uses nested switch case statements to check if the values of num1 and num2 match with any of the case labels. The output of this code will be Number is 3.

Summary

In this lab, you learned how to use the switch case statement in C with examples. You learned how to use the switch case statement with different data types, how to use it without the break statement, and how to nest switch case statements.

Other Tutorials you may like