Show contents
Chapter 1 of 6 · 20 min
if, else and switch
By the end of this chapter, you'll be able to
- Write a condition with if, else if and else
- Use the comparison operators and the logical operators &&, || and !
- Choose between several exact values with switch
- Tell assignment = apart from comparison ==
if, else if, else
#include <stdio.h>
int main(void) {
double temperature = 31.5;
if (temperature >= 30) {
printf("It's very hot, drink some water.\n");
} else if (temperature >= 18) {
printf("Pleasant temperature.\n");
} else {
printf("Take a jacket.\n");
}
return 0;
}
It's very hot, drink some water.
The condition is written between parentheses, and the block to run between curly braces. The program tests the conditions from top to bottom and runs only the first block whose condition is true. The final else, which is optional, catches every other case.
The comparison operators: == (equal), != (not equal), <, <=, >, >=.
In C, a condition is a number: 0 means false, and any other value means true. A comparison is worth 1 if it's true, 0 otherwise. For clarity, the <stdbool.h> header provides the type bool and the values true and false.
Combining conditions
&&(and): true if both conditions are true;||(or): true if at least one is true;!(not): inverts a condition.
The first block runs. The else block is skipped.
Text description of the animation
- A cinema lets adults in, or minors who are accompanied.
- age is 16.
- accompanied is true (that is, 1).
- We evaluate the left-hand side of the ||: 16 >= 18 is false.
- With ||, one true side is enough: we look at accompanied, which is true. The whole condition is true.
- The first block runs. The else block is skipped.
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int year = 2024;
bool leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
if (leap) {
printf("%d is a leap year\n", year);
}
if (!leap) {
printf("%d is not a leap year\n", year);
}
return 0;
}
2024 is a leap year
C evaluates && and || from left to right and stops as soon as the result is known: in false && …, the right-hand side isn't even computed.
switch: choosing between exact values
When you compare a single integer variable (or a char) with several precise values, switch is more readable than a long chain of else if:
#include <stdio.h>
int main(void) {
char choice = 'b';
switch (choice) {
case 'a':
printf("Check balance\n");
break;
case 'b':
printf("Withdraw money\n");
break;
default:
printf("Unknown choice\n");
}
return 0;
}
Withdraw money
Each case must end with break, which leaves the switch. Without it, execution "falls through" into the next case. With -Wextra, GCC points it out (this statement may fall through). default handles all the other values.
Your turn
Worked exercise
Worked exercise: the bus fare
A bus company charges these fares: free under 4, 300 FCFA from 4 to 11, 300 FCFA again from 65, and 500 FCFA otherwise. Write a program that stores an age in a variable age and displays the fare. Test it with 2, 8, 30 and 70.
Hint 1
age < 4.Hint 2
||: (age <= 11) || (age >= 65), once the under-4 case has been handled.Show the solution and common mistakes
#include <stdio.h>
int main(void) {
int age = 70;
if (age < 4) {
printf("Free\n");
} else if (age <= 11 || age >= 65) {
printf("300 FCFA\n");
} else {
printf("500 FCFA\n");
}
return 0;
}
300 FCFA
Common mistakes:
- Writing
4 <= age <= 11: in C, this first compares4 <= age(which is 0 or 1), then compares that 0 or 1 with 11. So it's always true. You must writeage >= 4 && age <= 11. - Putting the
age <= 11case first: a 2-year-old would pay 300 FCFA.
Summary
- 1if (condition) { … } else if (…) { … } else { … }: only the first true block runs.
- 2In C, 0 is false and any other value is true; <stdbool.h> provides bool, true and false.
- 3&& (and), || (or), ! (not) combine conditions, evaluated from left to right.
- 4switch compares an integer value with exact cases; end each case with break.
- 5= stores a value, == compares: -Wall catches the mix-up.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.