Switch Case Statement in C Programming

Switch Case Statement in C Programming: In this post, we will learn with examples how to use switch case statement and break keyword in C programming.

Switch case statement and if.. else if.. else statement are used for almost the same task, so if you have not read the tutorials before this, then definitely read them first. 

Switch case is also used like if.. else if .. else statement to control the execution flow of C program statements.

That’s why the switch case statement is also called Control Flow Statements or Decision Making Statements like if.. else if .. else statement.

You can also read: Type Casting In C Programming

Switch Case Syntax:

switch(variable or integer expression)
{
    case value1:
    {
        //statements;
        break;
    }
    case value2:
    {
        //statements;
        break;
    }
    case value3:
    {
        //statements;
        break;
    }
    default:
    {
        //statements;
    }
}

Switch Case Statement in C Programming

Switch Case Syntax Explanation:

Keep in mind that switch, case, default and break are all C keywords, so they are written in lowercase only.

Now as you are seeing in the above syntax that in the parenthesis ( ) of the switch, we write only the variable or any expression i.e. no condition is written in it like the parenthesis ( ) of the if. 

In the parenthesis ( ) of the switch, only such a variable is written which has a value of int or char data type. Similarly, the expression is also written in such a way that the result value of int or char data type comes.

The curly braces { } after the parenthesis ( ) of the switch are called the body or compund statement of the switch and all the cases of the switch come under this body.

With all the cases of the switch, there is a constant value of int or char type, which we also call the case label.

While writing the case value, keep in mind that the value of all the cases should be different i.e. unique and the colon ( : ) is used immediately after the case value.

The block { } is associated with the case as well and in this block all the statements and break keyword of the case come.

If you want to control more than one statement inside the case, then definitely use curly braces for the body of the case.

And if you want to control only one statement inside the case, then it is not necessary to put curly braces { } with the case, you can apply if you want.

After all the cases in the switch, the default (case) is used in the last, which works just like the else i.e. when the statements of any case will not run, then the statements of default are run.

Apart from this, like condition is not written with else, in the same way no constant value is written with default but yes colon ( : ) is definitely applied.

And like else, default is also optional, that is, if your program is not needed, then do not use default in the switch.

Switch Case Statement in C Programming

How does switch case work in C programming?

Switch case also works like if, else if …, else like first if condition is checked if it becomes false then else if followed by next else if and if none of the condition is true then In the last else statements run.

In the working process of a similar switch case, the variable or expression result value in the parenthesis ( ) of the switch is compared one by one with the value of all the cases whether that value is equal to the value of the switch or not.

Now the case whose value is equal to the value of the switch is considered true and all the statements inside it are run.

If the value of switch is not equal to the value of any case, then at last all the statements inside the default (case) are run (executed).

Switch Case Statement in C Programming

What is the use of break keyword in switch case?

The mistake that students make the most while using the switch case is that after writing the statements inside the case, they forget to write the break keyword.

While using the if, else if …, else statement, it happens that as the condition of the if or any else if becomes true, then the condition of the else if and else is not checked and neither their statements run.

But this does not happen in a switch case, when the value of a case becomes equal to the value of the switch, then assuming that case is true, all its statements run, but all subsequent case statements also run.

That is, as soon as a case is true, all subsequent cases automatically become true without comparing the value of the switch and their statements also run.

If you want that this should not happen while using switch case in your program, then for that you have to use break keyword after every case statement.

This happens that as soon as the break keyword is found after the statements of a case are run, then the flow of your program comes directly out of the switch because of the break keyword and the statements of the case after the true case do not run. Huh.

Switch Case Statement in C Programming

Without break keyword switch case example program:

switch(num)
{
    case 1:
    {
        printf("Statement 1\n");
    }
    case 2:
    {
        printf("Statement 2\n");
    }
    case 3:
    {
        printf("Statement 3\n");
    }        
    default:
    {
        printf("Default Statement");
    }
}

Switch Case Statement in C Programming

Output 1: If the value of num is 2 then

Statement 2
Statement 3
Default Statement

Output 2: If the value of num is 3 then

Statement 3
Default Statement

Output 3: If the value of num is 6 then

Default Statement

Switch Case Statement in C Programming

switch case example program with break keyword:

#include <stdio.h>
int main()
{
    int num1,num2,ans,choice;
 
    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("Enter Your Choice : ");
    scanf("%d",&choice);
 
    switch(choice)
    {
        case 1:
        {
            printf("Enter First Number : ");
            scanf("%d",&num1);
            printf("Enter Second Number : ");
            scanf("%d",&num2);
            ans=num1+num2;
            printf("Addition : %d",ans);
            break;
        }
        case 2:
        {
            printf("Enter First Number : ");
            scanf("%d",&num1);
            printf("Enter Second Number : ");
            scanf("%d",&num2);
            ans=num1-num2;
            printf("Subtraction : %d",ans);
            break;
        }
        default:
        {
            printf("You Entered Wrong Number");
        }
    }
 
    return 0;
}

Switch Case Statement in C Programming

Ouput 1:

1. Addition
2. Subtraction
Enter Your Choice : 1
Enter First Number : 23
Enter Second Number : 5
Addition : 28

Ouput 2:

1. Addition
2. Subtraction
Enter Your Choice : 2
Enter First Number : 20
Enter Second Number : 5
Subtraction : 15

Ouput 3:

1. Addition
2. Subtraction
Enter Your Choice : 5
You Entered Wrong Number

Switch Case Statement in C Programming

Conclusion

I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at praveen171086@gmail.com.

You can also visit my Hindi YouTube Channel.

Leave a Comment