switch case in c

switch case in C/C++, A Brief Explanation

Using the switch case in C language, we can resolve complex conditional and branching operations. It reduces the comparison of integral value in a long if-else statement.

The switch expression transfers the control to a specific part of the code which has written in the body of switch and case statements. The control transfer capability of the switch case provides an easy way to dispatch execution to different parts of code based on the value of the expression.

Syntax of switch case in C:

//Syntax of switch case

switch ( expression )  //selection-statement
{

case  constant-expression_1 :  //labeled-statement

    break;
    .
    .
    .
    .
    .
    .
    .

case  constant-expression_n :  //labeled-statement

    break;

default :
    statement

    break;

}

 

Flow diagram of switch case

flow diagram of switch case

How to switch and case statements work?

In switch case statement when you pass an integral argument then it searches the case constant-expression within its body. If the integral value matches the case statement then control directly jump to the case statement and execute the code until the end of the body or break statement. The break statement transfers the control out of the body.

In the switch case, we can use the break statement to end the execution of the code for the particular case. If we forgot to put the break statement after each case, the program continues to the next case until not getting a break or end of the code.

Note:  We can include any number of the case in switch statement but the integer constant expression for the case label must be different.

Let’s see an example code to understand the working concept of the switch statement in C programming.

#include <stdio.h>


//perform multiplication of two number
int Multiply()
{
    int input1 = 0;
    int input2 = 0;

    printf("\n\nEnter the two number !\n");
    scanf( "%d%d", &input1,&input2);
    return (input1 * input2);
}

//perform subtraction of two number
int Subtraction()
{
    int input1 = 0;
    int input2 = 0;

    printf("\n\nEnter the two number !\n");
    scanf( "%d%d", &input1,&input2);
    return (input1 - input2);
}

//perform addition of two number
int Addition()
{
    int input1 = 0;
    int input2 = 0;

    printf("\n\nEnter the two number !\n");
    scanf( "%d%d", &input1,&input2);
    return (input1 + input2);
}


int main()
{
    int iRet   = 0;
    int iChoice   = 0;

    printf( "1. Addition of two number\n" );
    printf( "2. Subtraction of two number\n" );
    printf( "3. Multiply of two number\n" );
    printf( "4. Exit\n" );

    printf("\n\nEnter your choice = ");
    scanf( "%d", &iChoice);


    switch (iChoice)
    {
    case 1:
        iRet = Addition();
        break;
    case 2:
        iRet = Subtraction();
        break;
    case 3:
        iRet = Multiply();
        break;
    case 4:
        printf("Exit from the calculator!\n" );
        break;
    default:
        printf( "Bad input!\n" );
        break;
    }

    printf("Computation Result = %d\n",iRet);

    return 0;
}

When you run the program, the output will be:

switch c

 

 

 

 

 

In this example, Every case has the break statements.If iChoice is equal to 1, the addition of two number is performed. After the execution of case 1, control comes to the break statement, it transfers the control out of the body, bypassing the remaining statements. Similarly, if iChoice is equal to 2, the subtraction of two number happens.

 

If you want to learn more about the C language, here 10 Free days C video course for you.

C tutorial

 

Important points related to switch-case in C/C++ programming

  • The expression of each case label shall be an integer constant expression and not be a floating number.
  • The value of case constant expressions must be unique within the same switch statement.
  • The sequence of case constant expressions does not affect the execution.
  • Using the macro Identifier, we can create case labels but it should be a unique and integral type.
  • The case labels must end with a colon (:).
  • Every switch case should have one default label. If we remove the default label and there is no case match found then no statements are executed. It creates confusion.
  • We can write the default statement anywhere in the body of the switch statement.
  • In some scenarios, many switch cases share only one break statement.
  • We should not use a comparison operator in the switch statement because the compassion statement evaluates only 1 or 0.Let’s see an code where I am using the comparison statement in switch statement.
#include <stdio.h>

int main()
{
    int iChoice  = 0;

    printf("Enter your choice = ");
    scanf( "%d", &iChoice);

    //here I have used comparison statement
    switch (iChoice < 20)
    {
    case 10:
        printf("Your enter choice is 1\n");
        break;

    case 20:
        printf("Your enter choice is 2\n");
        break;

    case 30:
        printf("Your enter choice is 3\n");
        break;

    default:
        printf("Bad Input !\n");
        break;

    }

    return 0;
}

 

Now times to see some programming example to understand the switch case and the points which I have described above. If you have any suggestions and queries, then please wire in the comment box.

 

1.  If there is no break statement implement within the switch body.

#include <stdio.h>

int main()
{
    int iChoice = 0;

    printf("Enter your choice = ");
    scanf( "%d",&iChoice);

    switch (iChoice)
    {
    case 1:
        printf("case 1 !\n");

    case 2:
        printf("case 2 !\n");

    case 3:
        printf("case 3 !\n");

    default:
        printf("default !\n" );
    }

    return 0;
}

When you run the program and your choice is 1, the output will be:

switch case in c example

 

 

 

 

When you run the program and your choice is 2, the output will be:

switch c code

 

 

 

Explaination: In above example, if iChoice is equal to 1, all statements of the body are executed because there is no break statement appear in the switch body. If ichoice equal to 2 then control jump to case 2 and execute all the below case since there is no break statement.

 

2. A single statement can carry multiple case labels.

#include <stdio.h>

void TestFunction(void)
{
    printf("Demo code\n");
}

int main()
{
    int iChoice = 0;

    printf("Enter your choice = ");
    scanf( "%d", &iChoice);

    switch ( iChoice )
    {
    case 1:
    case 2:
    case 3:
        //Calling function
        TestFunction();
        break;
    case 4:
        printf("Wow Test paas !");
        break;

    default:
        printf("default !\n" );
    }

    return 0;
}

Explanation: The TesFunction() will execute for cases 1,2 and 3.

 

3. If the switch case has the same case label.

#include <stdio.h>

int main()
{
    int iChoice   = 0;
    int i = 0;

    printf("Enter your choice = ");
    scanf( "%d", &iChoice);

    switch ( iChoice )
    {
    case 1:

        i++;
        break;

    case 3:

        i = i + 2;
        break;

    case 3:

        i = i + 3;
        break;

    default:
        printf("default !\n" );
        break;
    }

    printf("Value of i = %d",i);

    return 0;
}

When you run the program, the output will be:

case statement in c

 

 

4. If the switch case value is a floating number.

#include <stdio.h>

int main()
{
    int iChoice   = 0;
    int i = 0;

    printf("Enter your choice = ");
    scanf( "%d", &iChoice);

    switch (iChoice)
    {
    case 1:

        i++;
        break;

    case 2.5:

        i = i + 2;
        break;

    case 3:

        i = i + 3;
        break;

    default:
        printf("default !\n" );

    }

    printf("Value of i = %d",i);

    return 0;
}

 

switch case float

 

 

5. We can put default case anywhere within the body see below example program.

#include <stdio.h>

int main()
{
    int iChoice  = 0;

    printf("Enter your choice = ");
    scanf( "%d", &iChoice);

    switch (iChoice)
    {
    default:
        printf("Bad Input !\n");
        break;
    case 1:
        printf("Your enter choice is 1\n");
        break;
    case 2:
        printf("Your enter choice is 2\n");
        break;
    case 3:
        printf("Your enter choice is 3\n");
        break;

    }
    return 0;
}

When you run the program, the output will be:

switch statement default case

 

 

 

6. Case label should be a constant value in the switch case.

#include <stdio.h>

int main()
{
    int iChoice  = 0;
    int Label = 1;

    printf("Enter your choice = ");
    scanf( "%d", &iChoice);

    switch (iChoice)
    {

    case Label:

        printf("Your enter choice is 1\n");
        break;
    case 2:

        printf("Your enter choice is 2\n");
        break;
    case 3:

        printf("Your enter choice is 3\n");
        break;

    default:
        printf("Bad Input !\n");
        break;
    }
    return 0;
}

When you run the program, the output will be:

switch tutorial

 

 

7. We can implement a nested switch case in C programming.

#include <stdio.h>

void nestedSwitchDemo(int input1, int input2)
{
    switch (input1)
    {
    case 1:

        printf("Your enter choice is 1\n");
        switch (input2 )
        {
        case 1:
            printf("Enter Sub choice is 1\n");
            break;

        case 2:
            printf("Enter Sub choice is 2\n");
            break;
        }

        break;
    case 2:
        printf("Your enter choice is 2\n");
        break;

    case 3:
        printf("Your enter choice is 3\n");
        break;

    default:
        printf("Bad Input !\n");
        break;
    }
}


int main()
{
    int iChoice  = 1;
    int iSubChoice = 1;

    //Calling function
    nestedSwitchDemo(iChoice,iSubChoice);

    return 0;
}

 

Recommended Articles for you: