Control Flow: Decisions and Loops · Lesson 10 of 35
if, else and switch in C
Programs make decisions: pass or fail, discount or no discount, which menu option the user chose. C's decision-making statements are if, else if, else and switch. They're the first thing every lab exam tests – grading programs, largest of three numbers, calculators.
In this lesson
if,elseand the else-if ladder- Nested conditions and the dangling else
switchwithcase,breakanddefault- Choosing between if and switch
if and else
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0) {
printf("%d is even\n", n);
} else {
printf("%d is odd\n", n);
}
return 0;
}
Enter a number: 17 17 is odd
The else-if ladder
#include <stdio.h>
char grade_for(int marks) {
if (marks >= 90) return 'O';
else if (marks >= 75) return 'A';
else if (marks >= 60) return 'B';
else if (marks >= 40) return 'C';
else return 'F';
}
int main(void) {
int tests[] = {95, 76, 60, 39};
for (int i = 0; i < 4; i++) {
printf("%d marks -> grade %c\n", tests[i], grade_for(tests[i]));
}
return 0;
}
95 marks -> grade O 76 marks -> grade A 60 marks -> grade B 39 marks -> grade F
How the ladder is checked
- marks >= 90?true → 'O', the rest is skipped
- otherwise marks >= 75?true → 'A'
- …and so on downthe first true condition wins
- none truethe final else → 'F'
Nested if: largest of three
#include <stdio.h>
int main(void) {
int a = 42, b = 87, c = 63;
if (a >= b) {
if (a >= c) printf("Largest: %d\n", a);
else printf("Largest: %d\n", c);
} else {
if (b >= c) printf("Largest: %d\n", b);
else printf("Largest: %d\n", c);
}
return 0;
}
Largest: 87
Common mistakes
- A stray semicolon:
if (x > 0); { … }– the;is an empty statement, so the block always runs. - Missing braces: without braces only the first statement belongs to the if. Always use braces.
- Dangling else: an
elsebelongs to the nearest unmatchedif, whatever the indentation suggests. - Chained comparisons:
if (1 < x < 10)means(1 < x) < 10, which is always true. Writex > 1 && x < 10.
switch
switch compares one integer (or character) expression against constant values:
#include <stdio.h>
int main(void) {
double a = 12, b = 4;
char ops[] = {'+', '-', '*', '/', '%'};
for (int i = 0; i < 5; i++) {
char op = ops[i];
switch (op) {
case '+': printf("%.0f + %.0f = %.2f\n", a, b, a + b); break;
case '-': printf("%.0f - %.0f = %.2f\n", a, b, a - b); break;
case '*': printf("%.0f * %.0f = %.2f\n", a, b, a * b); break;
case '/':
if (b != 0) printf("%.0f / %.0f = %.2f\n", a, b, a / b);
else printf("Cannot divide by zero\n");
break;
default: printf("Unknown operator %c\n", op);
}
}
return 0;
}
12 + 4 = 16.00 12 - 4 = 8.00 12 * 4 = 48.00 12 / 4 = 3.00 Unknown operator %
Fall-through
Without break, execution continues into the next case. Usually that's a bug, but it's handy when several cases share code:
#include <stdio.h>
int main(void) {
int months[] = {2, 4, 7};
for (int i = 0; i < 3; i++) {
int m = months[i], days;
switch (m) {
case 2:
days = 28;
break;
case 4: case 6: case 9: case 11: // several cases, one block
days = 30;
break;
default:
days = 31;
}
printf("Month %d has %d days\n", m, days);
}
return 0;
}
Month 2 has 28 days Month 4 has 30 days Month 7 has 31 days
| Use if / else if when… | Use switch when… |
|---|---|
testing ranges (marks >= 75) | matching one value against fixed constants |
| conditions involve several variables | building menus and command handlers |
| comparing strings or doubles | the value is an int, char or enum |
Practice
Try it yourself
Read a character and say whether it's a vowel, a consonant or not a letter.
Show solution
#include <stdio.h> #include <ctype.h> int main(void) { char ch; printf("Enter a character: "); scanf(" %c", &ch); if (!isalpha((unsigned char)ch)) { printf("'%c' is not a letter\n", ch); } else { switch (tolower((unsigned char)ch)) { case 'a': case 'e': case 'i': case 'o': case 'u': printf("'%c' is a vowel\n", ch); break; default: printf("'%c' is a consonant\n", ch); } } return 0; }Enter a character: E 'E' is a vowel
Quick check
What happens if you leave out break in a case?
Execution falls through into the next case.
Can a switch case use a string or a range?
No – case labels must be integer constant expressions (ints, chars, enums).
Which if does an else belong to?
The nearest unmatched if before it.
Summary
- if/else if/else tests conditions in order; the first true branch runs.
- Always use braces; watch for stray semicolons and chained comparisons.
- switch matches an integer against constant cases; remember break and default.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.