Loading...
C switch case

C switch case

In this tutorial, we will learn about switch statement and its working in C programming with the help of an example.


Switch Statement

The switch statement is used to execute different parts of code based on the value of expression.

In simple words, Switch statements are helpful when we have a set of multiple conditions and the user needs to select one of them based on the matching condition.

Note :

switch (expression)
​{
  case constant1:
  // statements
  break;
  case constant2:
  // statements
  break;
  .
  .
  default:
  // default statements
}

How does the switch statement work?

  • The expression is evaluated once and compared with the values of each case label. If the any case matches then the block of code associated with that case is executed.
  • Then, the break is encountered, the execution of that case stops and the switch statement terminates.
  • However, if any case does not match, then default case is executed.

Note : Switch case statement is control statement that is regarded as a substitute for if-else statements.
If we do not use break, all statements after the matching label are executed.
By the way, the default case inside the switch statement is optional.

We can do the same thing with the if...else..if ladder. However, the syntax of the switch statement is cleaner and much easier to read and write.


switch Statement Flowchart

C Flowchart of switch statement
Flowchart of switch statement in C

Significance of “default” keyword

The default keyword is responsible for executing the piece of code if no matching case labels are found.


Choosing data type for the case labels in switch statements

  • The data type of case labels of the switch statement in C++ can be either an integer or character type.
  • The case label can be either a constant variable or a constant expression.

Example: Simple Calculator

// C Program to create a simple calculator
#include <stdio.h>
int main() {
  char operator;
  double num1, num2;
  printf("Enter an operator (+, -, *, /): ");
  scanf("%c", &operator);
  printf("Enter two operands: ");
  scanf("%lf %lf",&num1, &num2);
  switch(operator)
  case '+':
    printf("%.1lf + %.1lf = %.1lf",num1, num2, num1+num2);
    break;
  case '-':
    printf("%.1lf - %.1lf = %.1lf",num1, num2, num1-num2);
    break;
  case '*':
    printf("%.1lf * %.1lf = %.1lf",num1, num2, num1*num2);
    break;
  case '/':
    printf("%.1lf / %.1lf = %.1lf",num1, num2, num1/num2);
    break;
  default:
    printf("Error! Please enter a valid operator");
  }
  return 0;
}

Output 1 :

Enter an operator (+, -, *, /): +
Enter two numbers: 5 3
5 + 3 = 8

Output 2 :

Enter an operator (+, -, *, /): -
Enter two numbers: 5 3
5 - 2 = 3

Output 3 :

Enter an operator (+, -, *, /): *
Enter two numbers: 5 2
5 * 2 = 10

Output 4 :

Enter an operator (+, -, *, /): /
Enter two numbers: 6 2
6 / 2 = 3

Points to Remember

  1. The expression provided in the switch must be a constant value otherwise it is invalid.
  2. Duplicate case values are not allowed.
  3. The default statement is optional. Even if the switch case statement do not have a default statement, it would work without any problem.
  4. The break statement is used to take control out of the loop otherwise all the cases before a break would be executed.
  5. break statement is used inside the switch to terminate a statement sequence. When a break statement is reached , the switch terminates, and the flow of control jumps to next line following the switch statement.
  6. The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall though to subsequent cases until a break is reached.
  7. The switch statement can also be nested, which means you have switch statements inside another switch.

To know more about Break statement, Click Here.


Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Switch-Case Statement in C.

Keep Learning : )

In the next tutorial, you'll learn about C Goto Statement.

- Related Topics