Skip to content
StudyFame

switch vs if-else in C

Both switch and if-else make decisions. So which one should you use? And what are the differences? This is one of the most common comparison questions in C exams and interviews.

In this lesson you will see a clear comparison table, the same program written both ways, and simple rules to choose the right one.

What You Will Learn

  • The key differences between switch and if-else.
  • What each one can and cannot do.
  • The same program written with both.
  • Simple rules for choosing.

switch vs if-else in C

Exam definition: if-else can test any condition (ranges, relational and logical expressions, floats, strings via functions). switch tests one integer or character expression for equality against fixed constant values, and is cleaner when there are many such values.

Difference between switch and if-else

Pointif-else / else-if ladderswitch
What it testsAny condition: >, <, &&, ||, rangesOnly equality with constants
Data typesAll types (int, float, char, pointers...)Only int, char, enum
Ranges (e.g. 80–89)YesNo (each value needs its own case)
Values compared withVariables or constantsConstants only
Many fixed optionsLong and repetitiveShort and readable
Speed with many optionsChecks conditions one by oneOften faster (compiler can jump directly)
Default caseFinal elsedefault
Fall-throughNoYes, if break is missing

Memory trick: if-else is a judge: it can decide any question ("Is the marks above 80? Is the name Riya?"). switch is a receptionist with a fixed list: "Floor 1? Go here. Floor 2? Go there." It only matches exact numbers or letters.

Rules for choosing

  • Checking a range or complex condition? → if-else.
  • Checking a float, double or string? → if-else.
  • Choosing among many exact integer or character values (menus, day numbers, operators like +, −)? → switch.

Syntax Side by Side

/* if-else ladder */                 /* switch */
if (x == 1)                          switch (x)
    printf("One");                   {
else if (x == 2)                         case 1: printf("One"); break;
    printf("Two");                       case 2: printf("Two"); break;
else                                     default: printf("Other");
    printf("Other");                 }

Example 1: The Same Program with switch and with if-else

A simple calculator that takes an operator character (+, −, *, /).

#include <stdio.h>

int main(void)
{
    int a, b;
    char op;

    printf("Enter expression (e.g. 8 * 6): ");
    scanf("%d %c %d", &a, &op, &b);

    /* Version 1: switch */
    switch (op)
    {
        case '+': printf("switch : %d\n", a + b); break;
        case '-': printf("switch : %d\n", a - b); break;
        case '*': printf("switch : %d\n", a * b); break;
        case '/': printf("switch : %d\n", a / b); break;
        default:  printf("switch : invalid operator\n");
    }

    /* Version 2: if-else ladder */
    if (op == '+')
        printf("if-else: %d\n", a + b);
    else if (op == '-')
        printf("if-else: %d\n", a - b);
    else if (op == '*')
        printf("if-else: %d\n", a * b);
    else if (op == '/')
        printf("if-else: %d\n", a / b);
    else
        printf("if-else: invalid operator\n");

    return 0;
}

Output

Enter expression (e.g. 8 * 6): 8 * 6
switch : 48
if-else: 48

Code Explanation

  1. scanf("%d %c %d", ...) reads a number, an operator character and another number.
  2. Both versions give the same answer. Here we compare one char with fixed values, so switch is neater: every case is one clean line.
  3. The if-else version repeats op == four times.

How the Compiler Treats Them

  • An else-if ladder is checked one condition at a time, from top to bottom.
  • For a switch with many cases, the compiler can build a jump table and go directly to the right case, so it can be faster.
  • For just 2 or 3 options, the speed difference does not matter. Choose whichever is clearer.

Example 2: Where Only if-else Works (Ranges)

#include <stdio.h>

int main(void)
{
    float temp;

    printf("Enter temperature: ");
    scanf("%f", &temp);

    if (temp >= 40)
        printf("Very hot\n");
    else if (temp >= 30)
        printf("Hot\n");
    else if (temp >= 20)
        printf("Pleasant\n");
    else
        printf("Cold\n");

    return 0;
}

Output

Enter temperature: 36.5
Hot

A switch cannot do this: the value is a float, and we are checking ranges, not exact values.

Important Points to Remember

  • if-else: any condition, any type, ranges allowed.
  • switch: only equality, only int/char, constant labels.
  • switch is cleaner and often faster for many fixed values.
  • switch can fall through; if-else cannot.
  • Every switch can be written as if-else, but not every if-else can be a switch.

Common Mistakes Students Make

Wrong ideaCorrect idea
switch can check ranges like case 1..10Standard C has no ranges in case labels
switch works with stringsNo; use if-else with strcmp()
switch is always fasterOnly noticeably with many cases
if-else can replace switch only sometimesif-else can always replace switch

switch vs if-else Practice Questions

  1. Write four differences between switch and if-else.
  2. Can every switch statement be written as an if-else? Can every if-else be written as a switch?
  3. Which would you use to check if marks are between 60 and 75?
  4. Which would you use for a menu with options 1 to 6?
  5. Why can a switch be faster than an else-if ladder?
Show answers
  1. if-else tests any condition, switch only equality; if-else works with all types, switch only int/char; if-else allows ranges, switch doesn't; switch can fall through, if-else can't.
  2. Yes, every switch can be written as if-else. No, not every if-else can be a switch (ranges, floats, complex conditions).
  3. if-else (marks >= 60 && marks <= 75).
  4. switch.
  5. The compiler can use a jump table to go directly to the matching case instead of checking each condition one by one.

Coding Exercise

Read a grade letter (A, B, C, D or F) and print a remark ("Excellent", "Good", "Average", "Poor", "Fail") using switch. Explain why switch suits this problem.

Show solution
#include <stdio.h>

int main(void)
{
    char grade;

    printf("Enter grade: ");
    scanf(" %c", &grade);

    switch (grade)
    {
        case 'A': printf("Excellent\n"); break;
        case 'B': printf("Good\n");      break;
        case 'C': printf("Average\n");   break;
        case 'D': printf("Poor\n");      break;
        case 'F': printf("Fail\n");      break;
        default:  printf("Invalid grade\n");
    }
    return 0;
}
Enter grade: B
Good

switch suits it because we compare one char with a few exact values.

Quick Quiz: switch vs if-else (MCQ)

  1. Which can check a range of values?

    A) switch   B) if-else   C) Both   D) Neither

  2. Which can test a float value?

    A) switch   B) if-else   C) Both   D) Neither

  3. Fall-through can happen in:

    A) if-else   B) switch   C) Both   D) Neither

  4. For a menu with choices 1–10, the cleaner choice is:

    A) switch   B) Nested if   C) goto   D) Ternary

  5. The if-else equivalent of default is:

    A) else if   B) The final else   C) break   D) continue

Show answers
  1. B: if-else.
  2. B: if-else.
  3. B: switch.
  4. A: switch.
  5. B: the final else.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

if-else testsAny condition, any type, ranges
switch testsEquality only, int/char, constants
Rangesif-else only
Floats and stringsif-else only
Many fixed valuesswitch is cleaner (often faster)
Fall-throughswitch only
ReplaceabilityEvery switch → if-else; not the reverse

Frequently asked questions

Which is better, switch or if-else?
switch for many exact int/char values; if-else for ranges and complex conditions.
Why can't switch check ranges?
Case labels must be single constant values; switch only tests equality.
Is switch faster than if-else?
Often, with many cases, because of a jump table; for a few cases, no real difference.

Remember

  • if-else = judge (any question); switch = receptionist (fixed list).
  • Range, float or string → if-else.

Next Topic

Next lesson: goto Statement in C. You will learn the goto statement, and why most programmers avoid it.