C - Switch Case decision making statements

C Programming


In this tutorial we will learn about switch-case decision making statements in C programming language.

Switch case statements are somewhat similar to the if else statement.

Syntax of a switch-case statement.

switch (expression) {
  case value1:
    //block 1 code...

  case value2:
    //block 2 code...

    //default block code...

So, in the above syntax of a switch-case statement we have an expression which is matched with the case values value1, value2 and so on.

If lets say, the expression matches the case value2 then the block-2 code is executed and others are ignored.

The break keyword takes us out of the switch. It is recommended to have the break statement in the case block in order to jump out of the switch-case statement.

If no case value matches the expression then the default block code is executed.

Note! The default block is optional and can be skipped. And we don't have to add the break keyword in the default block.

Points to remember

  • expression is an integer or character
  • value1, value2, ... are integer constant and are called case label
  • block1, block2, ... contain zero or more statements
  • default is an optional case. If no case label matches the expression then the default-statement is executed
  • break statement signals the end of a particular case and causes an exit from the switch-block

In the following example we will take an integer value from the user as input and then match the value with the cases to print the suitable result.

#include <stdio.h>
int main(void)
  //declare variable
  int x;
  //take user input
  printf("Enter an integer number: ");
  scanf("%d", &x);
  //check condition
  switch (x) {
    case 1:
      printf("Entered number: 1\n");
    case 2:
      printf("Entered number: 2\n");
    case 3:
      printf("Entered number: 3\n");
      printf("Entered number is something else.\n");
  printf("End of code\n");
  return 0;


Enter an integer number: 1
Entered number: 1
End of code

In the above output we have entered 1 as input so, in the switch-case statement the value of x (expression) is matched with case 1. Hence, the case 1 block is executed. And because of the break keyword we jump out of the switch.

Output: When entered number is something else.

Enter an integer number: 10
Entered number is something else.
End of code

In the above output we have entered 10 as input value and no case value is a match. So, we execute the default block code.