Switch Case Statement in C Programming

Switch case statement in c programming; In this tutorial, i am going to show you what is switch case statement and how to use switch case statement in c programming programs.

Switch Case Statement in C Programming

The c programming switch case statement is used to test multiple conditions and perform a different task for each conditions.

Syntax of goto Statement in c programming

Syntax of switch case statement in c; as shown below:

switch (variable or an integer expression)
{
     case constant:
     //C Statements
     ;
     case constant:
     //C Statements
     ;
     default:
     //C Statements
     ;
}

Example 1 – Simple C Program using Switch Case Statement

Let’s take an first example using switch case statement in c; as shown below:

#include <stdio.h>
int main()
{
     int num=2;
     switch(num+2)
     {
         case 1:
           printf("Case1: Value is: %d", num);
         case 2:
           printf("Case1: Value is: %d", num);
         case 3:
           printf("Case1: Value is: %d", num);
         default:
           printf("Default: Value is: %d", num);
    }
    return 0;
}

Output

15

Example 2 – Simple Calculator c Program using Switch Case

Let’s create simple calculator c program using switch case; as shown below:

#include <stdio.h>
int main() {
  char op;
  double first, second;
  printf("Enter an operator (+, -, *, /): ");
  scanf("%c", &op);
  printf("Enter two operands: ");
  scanf("%lf %lf", &first, &second);
  switch (op) {
    case '+':
      printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
      break;
    case '-':
      printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
      break;
    case '*':
      printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
      break;
    case '/':
      printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
      break;
    // operator doesn't match any case constant
    default:
      printf("Error! operator is not correct");
  }
  return 0;
}

Output

Enter an operator (+, -, *,): *
Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8

Example 3 – Simple c Program using Switch Case without Break Statement

Let’s create simple c program using switch case without break statement; as shown below:

#include<stdio.h>
int main()
{
    /* Local Variable Definition */
    char grade;
    printf("Enter your grade (Like A, B, C):\n");
    scanf("%c", &grade);
    switch(grade)
    {
        case 'A':
            printf("Excellent\n");
        case 'B':
            printf("\n\n\nKeep it up!\n\nNo break statement\n\nHence all the case following this(but not the ones above this) except the default case will get executed !\n\n");
        case 'C':
            printf("\n\n\t\tCase C : Well done !\n\n");
        case 'D':
            printf("\t\tCase D : You passed!\n\n");
        case 'F':
            printf("\t\tCase E : Better luck next time\n\n\n");
        default:
            printf("\t\tDefault Case : Invalid grade\n\n\n");
    }
    printf("Your grade is %c\n",grade);
    printf("\n\n\t\t\t Happy coding !\n\n\n");
    return 0;
}

Output

/tmp/AuYmOiRg3h.o
Enter your grade (Like A, B, C):
A
Excellent



Keep it up!

No break statement

Hence all the case following this(but not the ones above this) except the default case will get executed !



		Case C : Well done !

		Case D : You passed!

		Case E : Better luck next time


		Default Case : Invalid grade


Your grade is A


			 Happy coding !


More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.