Switch statement in C

In this article, we will discuss Switch Statement in C which provides an easy way to execute different parts of the codes based on the values.

The switch statement in C is an alternative to the if-else-if ladder statement in which multiple operations for the different possible values of a single variable.

Switch statement in C

Syntax

switch( variable/expression)
{
  case value1:
             //block of code or statement to be executed;
             break; //optional
  case value2:
             //block of code or statement to be executed;
             break; //optional
  .....
  case valuen:
             //block of code or statement to be executed;
             break; //optional

  default:
           // Code if all cases not matched

Rules for switch statement in C language

  • The switch expression must be of an integer or character type and case value integer constant or character constant only.
  • The case value can be used only inside the switch statement.
  • The break statement is optional.
  • If break statement is not found in the case, all the cases will be executed after the matched case and switch statement in C without break is called as fall through which is the second type of switch statement in C.

Flow Chart of Switch Statement in C

Flowchart of the Switch statement in C

Functioning – switch case statement in C

  • The expression is evaluated once and matched with the values of different case values.
  • If matched, the statements after the matched case is executed.
  • If break is find in the matched case block, all the cases after the matched one will be skipped.
  • If break is not found, all the cases after the matched one will be executed (Fall through)
  • If there is no match, the default statements are executed.

Let’s understand the same with some examples of a switch statement in c language

Example-1

#include<stdio.h>  
int main()
{    
    int num=0;     
    switch( variable/expression)
{
  case value1:
             //block of code or statement to be executed;
             break; //optional
  case value2:
             //block of code or statement to be executed;
             break; //optional
  .....
  case valuen:
             //block of code or statement to be executed;
             break; //optional

  default:
           // Code if all cases not matchedprintf("enter a number:");    
    scanf("%d",&num);    
    switch(num)
    {    
        case 10:
            printf("number is equals to 10");    
            break;    
        case 50:    
            printf("number is equal to 50");    
            break;    
        case 100:    
            printf("number is equal to 100");    
            break;    
        default:    
            printf("number is not equal to 10, 50 or 100");    
    }       
    return 0;  
} 

Output

enter a number:4
number is not equal to 10, 50 or 100

//Second run with different value

enter a number:10
number is equal to 10

Example-2 Simple Calculator

// Program to create a simple calculator
#include <stdio.h>

int main() {
    char operator;
    double n1, n2;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);
    printf("Enter two operands: ");
    scanf("%lf %lf",&n1, &n2);

    switch(operator)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
            break;

        case '-':
            printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
            break;

        case '*':
            printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
            break;

        case '/':
            printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
            break;

        // operator doesn't match any case constant +, -, *, /
        default:
            printf("Error! operator is not correct");
    }

    return 0;
}

Output

Enter an operator (+, -, *,): +
Enter two operands: 15.0
5.0
15.0 + 5.0 = 20.0

Example-3 Fall through example (Without break)

#include<stdio.h>  
int main()
{
    int number=0;    
    printf("enter a number:");  
    scanf("%d",&number);  
  
    switch(number)
    {  
        case 10:  
            printf("number is equal to 10\n");  
        case 50:  
            printf("number is equal to 50\n");  
        case 100:  
            printf("number is equal to 100\n");  
        default:  
            printf("number is not equal to 10, 50 or 100");  
    }  
    return 0;  
}  

Output

enter a number:10
number is equal to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100

Try executing your code’s here with different values

For any suggestions, please do not hesitate to comment below or contact me

You Might Like:

1 thought on “Switch statement in C”

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Please Reload After Disabling Ad\'s