Open In App

Difference Between if-else and switch in C

Last Updated : 25 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C programming both switch statements and if-else statements are used to perform decision-making and control the flow of the program according to predefined conditions. In this article, we will discuss the differences between the if-else and switch statements.

switch Statement

A control flow statement called a switch statement enables a program to compare an expression to a set of potential constant values by managing many scenarios according to the expression's value. When handling several potential scenarios, the switch statement makes the code easier to comprehend and maintain.

Syntax of switch Statement

switch (expression) {
case value1:
break;
case value2:
break;
default:
}

Example of switch Statement

The below example demonstrates the implementation of switch statement.

C
// C program to demonstrate the implementation of switch
// statement.
#include <stdio.h>
int 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");
    }
    return 0;
}

Output
Wednesday

if-else Statement

Conditional control structure called if-else statements are used to allow the execution of a particular code blocks on the basis that the given condition results in true of false. By running code in this way i.e. selectively according to whether a certain condition is true or false, we can easily make judgements.

Syntax of if-else Statement

if (condition) {
//code
}
else {
//code
}

Example of if-else

The below example demonstrates the implementation of if-else statement.

C
// C program to demonstrate the implementation of if-else
// statement.
#include <stdio.h>

int main()
{
    int number = 5;

    if (number > 0) {
        printf("The number %d is positive.\n", number);
    }
    else if (number < 0) {
        printf("The number %d is negative.\n", number);
    }
    else {
        printf("The number is zero.\n");
    }
    return 0;
}

Output
The number 5 is positive.

Difference Between if-else and switch Statement

The main differences between if-else and switch statement are listed in the following table:

S. No.

if-else Statement

switch Statement

1

The condition which used in if-else can be any Boolean expression.

The expression in switch is usually an integral type or an enumerated type.

2

It can handle multiple conditions using else if clauses.

Designed for evaluating a single expression against multiple constants.

3

if-else doesn't have fall-through behavior by default.

switch can have fall-through behavior (case statements without breaks).

4

Optional else block for handling cases not explicitly covered.

Optional default case for handling cases not explicitly covered.

5

Suitable for complex, non-trivial conditions.

More concise for handling a series of conditions based on a single expression.


Next Article

Similar Reads