switch case in C

  • What is switch case in c ?
  • The switch statement in C is a control structure used to execute one block of code among many based on the value of an expression. It allows you to compare the value of a variable or expression against a series of constants and execute different blocks of code depending on which constant matches the value.

  • Syntax :
  • switch (expression) {
    case constant1:
    // code to be executed if expression equals constant1
    break;
    case constant2:
    // code to be executed if expression equals constant2
    break;
    // you can have any number of case statements
    default:
    // code to be executed if expression doesn't match any case
    }

  • Key Points :
  • expression : The variable or expression whose value is compared against the case constants.
    case constant : A constant value that the expression is compared to.
    break : Exits the switch statement. If omitted, execution will continue to the next case (fall-through).
    default : Optional. Executes if no case matches the expression.

  • Examples :
  • C Code
    #include <stdio.h> main() { int day = 3; switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; default: printf("Invalid day\n"); break; } }

    Explanation :
    - The variable day is set to 3.
    - The switch statement compares day to each case.
    - When day is 3, the code in case 3: executes, printing "Wednesday".
    - The break statement prevents the execution from falling through to the next cases.
    - If day were not between 1 and 7, the default case would execute, printing "Invalid day".

  • Problems & Solutions :
  • C Code
    # Write a Program to accept a month and display the number of days present in that month. #include<stdio.h> int main() { int ch; printf("1.Jan\n"); printf("2.Feb\n"); printf("3.Mar\n"); printf("4.Apr\n"); printf("5.May\n"); printf("6.Jun\n"); printf("7.Jul\n"); printf("8.Aug\n"); printf("9.sep\n"); printf("10.oct\n"); printf("11.Nov\n"); printf("12.dec\n"); printf("Enter month no :"); scanf("%d",&ch); switch(ch) { case 1: printf("31 days"); break; case 2: printf("28 days"); break; case 3: printf("31 days"); break; case 4: printf("30 days"); break; case 5: printf("31 days"); break; case 6: printf("30 days"); break; case 7: printf("31 days"); break; case 8: printf("31 days"); break; case 9: printf("30 days"); break; case 10: printf("31 days"); break; case 11: printf("30 days"); break; case 12: printf("31 days"); break; default: printf("Invalid choice"); break; } }

    Output :
    Enter month no :5
    31 days

    C Code
    # Using switch case statement, write a menu driven program to convert a given temperature from Fahrenheit to Celsius and vice versa. #include<stdio.h> main() { int n,c,a; float f; printf("1.fah to cel\n"); printf("2.cel to fah\n"); printf("enter choice:"); scanf("%d",&n); switch(n) { case 1: printf("enter fah val:"); scanf("%d",&a); c=(a-32)/5*9; printf("%d",c); break; case 2: printf("enter cel val:"); scanf("%d",&a); f=1.8*a+32; printf("%f",f); break; default: printf("invalid choice"); break; } }

    Output :
    1.fah to cel
    2.cel to fah
    enter choice:1
    enter fah val:200
    297

    C Code
    # Write a program to display the colour of the spectrum (VIBGYOR) according to the User’s choice. V=violet I=indigo B=blue G=green Y= yellow O= orange R=red #include<stdio.h> main() { int ch; printf("VIBGYOR\n"); printf("Enter colour : "); scanf("%c",&ch); switch(ch) { case 'V': printf("violet"); break; case 'I': printf("indigo"); break; case 'B': printf("blue"); break; case 'G': printf("green"); break; case 'Y': printf("yellow"); break; case 'O': printf("orange"); break; case 'R': printf("red"); break; default: printf("invalid choice"); break; } }

    Output :
    VIBGYOR
    Enter colour : R
    red

  • Conclusion :
  • The switch statement in C is a powerful control structure that enhances code readability, maintainability, and efficiency. It is particularly useful when dealing with a variable that can take on multiple discrete values. By centralizing all related conditions, it provides a clear and organized way to handle complex branching logic.