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
| Point | if-else / else-if ladder | switch |
|---|---|---|
| What it tests | Any condition: >, <, &&, ||, ranges | Only equality with constants |
| Data types | All types (int, float, char, pointers...) | Only int, char, enum |
| Ranges (e.g. 80–89) | Yes | No (each value needs its own case) |
| Values compared with | Variables or constants | Constants only |
| Many fixed options | Long and repetitive | Short and readable |
| Speed with many options | Checks conditions one by one | Often faster (compiler can jump directly) |
| Default case | Final else | default |
| Fall-through | No | Yes, 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
scanf("%d %c %d", ...)reads a number, an operator character and another number.- Both versions give the same answer. Here we compare one
charwith fixed values, so switch is neater: every case is one clean line. - 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 idea | Correct idea |
|---|---|
switch can check ranges like case 1..10 | Standard C has no ranges in case labels |
| switch works with strings | No; use if-else with strcmp() |
| switch is always faster | Only noticeably with many cases |
| if-else can replace switch only sometimes | if-else can always replace switch |
switch vs if-else Practice Questions
- Write four differences between switch and if-else.
- Can every switch statement be written as an if-else? Can every if-else be written as a switch?
- Which would you use to check if marks are between 60 and 75?
- Which would you use for a menu with options 1 to 6?
- Why can a switch be faster than an else-if ladder?
Show answers
- 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.
- Yes, every switch can be written as if-else. No, not every if-else can be a switch (ranges, floats, complex conditions).
- if-else (
marks >= 60 && marks <= 75). - switch.
- 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)
-
Which can check a range of values?
A) switch B) if-else C) Both D) Neither
-
Which can test a
floatvalue?A) switch B) if-else C) Both D) Neither
-
Fall-through can happen in:
A) if-else B) switch C) Both D) Neither
-
For a menu with choices 1–10, the cleaner choice is:
A) switch B) Nested if C) goto D) Ternary
-
The if-else equivalent of
defaultis:A)
else ifB) The finalelseC)breakD)continue
Show answers
- B: if-else.
- B: if-else.
- B: switch.
- A: switch.
- 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 tests | Any condition, any type, ranges |
|---|---|
| switch tests | Equality only, int/char, constants |
| Ranges | if-else only |
| Floats and strings | if-else only |
| Many fixed values | switch is cleaner (often faster) |
| Fall-through | switch only |
| Replaceability | Every 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.