Switch Statements in C

Switch Statements in C Language

In this article, I am going to discuss the Switch Statements in C Language in detail. Please read our previous articles, where we discussed If else Statements in C with examples. At the end of this article, you will understand what is Switch statement in C and when and how to use switch statements in C Programming Language.

Switch Statements in C Program:

The switch is a keyword, by using the switch keyword we can create selection statements with multiple blocks. Multiple blocks can be constructed by using a “case” keyword.

Switch case statements are a substitute for long if statements that compare a variable to several integral values. The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The switch is a control statement that allows a value to change control of execution.

Rules for switch statement in C:
  1. The expression provided in the switch should result in a constant value otherwise it would not be valid.
  2. Duplicate case values are not allowed.
  3. The default statement is optional. Even if the switch case statement does not have a default statement,
    it would run without any problem.
  4. The 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 the next line following the switch statement.
  5. The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached.
  6. Nesting of switch statements is allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes the program more complex and less readable.

switch statement control flow in C

Syntax of switch statements in C:

Syntax of switch statements in C

After the end of each block it is necessary to insert a break statement because if the programmers do not use the break statement, all consecutive blocks of codes will get executed from every case onwards after matching the case block.

When do we need to go for a switch statement?

When there are several options and we have to choose only one option from the available options depending on a single condition then we need to go for a switch statement. Depending on the selected option a particular task can be performed.

Program to understand switch statement in C:
#include <stdio.h> 
int main() 
{ 
   int x = 2; 
   switch (x) 
   { 
       case 1: printf("Choice is 1"); 
               break; 
       case 2: printf("Choice is 2"); 
               break; 
       case 3: printf("Choice is 3"); 
               break; 
       default: printf("Choice other than 1, 2 and 3"); 
               break;
   } 
   return 0; 
}

Output: Choice is 2

What is the difference between nested if-else and switch statements?

By using nested if-else also we can also create multiple blocks whenever required, but for creating “n” no of blocks we required to create “n-1” conditions. In the switch statement, we can create multiple blocks under single condition that reduces the coding part.

When we are working with nested if-else at even any point of time among those all blocks only one block gets executed. But in the switch statement, we can create more than one block depending on the requirement by removing the break statement between the blocks.

Some tricky questions related to Switch Statement in C.

Question1: What will be the output in the below program?
#include <stdio.h>
int main()
{
    int i;
    i = 3;
    switch(i)
    {
        case 1:
            printf("A");
            break;
        case 3:
            printf("C");
        case 2:
            printf("B");
            break;
        default:
            printf("D");
    }
    return 0;
}

Output: CB

This is because whenever we are working with switch statements randomly we can create the cases i.e. in any sequences it can be created. In order to execute the switch block, it can be executed all cases in sequence from the matching case onwards until it finds the break statement.

Question2: What will be the output in the below program?
#include <stdio.h>
int main()
{
    int i;
    i = 5;
    switch(i)
    {
        case 1:
            printf("A");
            break;
        default:
            printf("D");
        case 2:
            printf("B");
            break;
        case 3:
            printf("B");
    }
}

Output: DB

This is because when are working with the default it can be placed anywhere within the switch body i.e. top of the switch statement or middle of the switch statement or end of the switch statement but recommended to place at end of the switch body. Placing the default is always optional, it is required to place whenever we are not handling all statements of the switch body.

Question3: What will be the output in the below program?
#include <stdio.h>
int main()
{
    float i;
    i = 2; //i = 2.0
    switch(i)
    {
        case 1:
            printf("A");
            break;
        case 2:
            printf("B");
            break;
        case 3:
            printf("C");
            break;
        default:
            printf("D");
    }
    return 0;
}

Output:

This is because whenever we are working with the switch statement it required condition and expression of type integer only i.e. float data we cannot pass within the switch.

Question4: What will be the output in the below program?
#include <stdio.h>
int main()
{
    int i;
    i = 2; 
    switch(i)
    {
        case 1.0:
            printf("A");
            break;
        case 2.0:
            printf("B");
            break;
        case 3.0:
            printf("C");
            break;
        default:
            printf("D");
    }
    return 0;
}

Output:

This because the case keyword required condition or expression of integer type value only i.e. we cannot pass float data as a case constant value.

Question5: What will be the output in the below program?
#include <stdio.h>
int main()
{
    int a = 1, b = 2, c = 3;
    int d = c-a;
    switch(d)
    {
        case a:
            printf("A1");
            break;
        case b:
            printf("B2");
            break;
        case c:
            printf("C3");
            break;
        default:
            printf("D4");
    }
    return 0;
}

Output:

Question6: What will be the output in the below program?
#include <stdio.h>
int main()
{
    int i;
    i = 3-2;
    switch(i)
    {
        case 2%2:
            printf("A");
            break;
        case 5/2:
            printf("B");
            break;
        case 3*2-3-2:
            printf("C");
            break;
        default:
            printf("D");
    }
    return 0;
}

Output: C

This is because when we are passing expression format data then it works according to the return type of the expression.

Question7: What will be the output in the below program?
#include <stdio.h>
int main()
{
    int i;
    i = 5 < 8;
    switch(i)
    {
        case 2>5:
            printf("A");
            break;
        case !2 != 2:
            printf("B");
            break;
        case 8 < 5:
            printf("C");
            break;
        default:
            printf("D");
    }
    return 0;
}

Output:

This is because in switch statements we cannot create more than one case with the same constant value. If we are creating then the compiler will give an error called duplicate case.

Points Remember when working with C Switch Statement:
  1. When we are working with the switch statement at the time of compilation switch condition/expression return value will match with case constant value. At the time of execution if the matching case occurs then control will pass to correspondent block, from matching case up to break everything will be executed, if the break does not occur then including default all cases will be executed.
  2. At the time of execution if the matching case does not occur then control will pass to default block. Default is a special kind of case that will be executed automatically when the matching case does not occur. Using default is always optional, it is recommended to use when we are not handling all cases of the switch block.
  3. When we are working with the switch it required expression or condition as a type of integer only.

In the next article, I am going to discuss While Loop in C with examples. Here, in this article, I try to explain Switch Statements in C Langauge with examples. I hope you enjoy this Switch Statements in C Language article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *