Making decisions: if and switch
Make your program choose what to do with if, else, logical operators and switch.
- Branch with
if,else ifandelse - Combine conditions with
&&,||and! - Use
switchfor many fixed cases - and rememberbreak
So far, programs have run every line top to bottom. Control flow lets a program choose which lines run. The simplest tool is if: run a block only when a condition is true.
if, else if, else
1#include <stdio.h>
2
3int main(void) {
4 int score = 83;
5
6 if (score >= 90) {
7 printf("A\n");
8 } else if (score >= 80) {
9 printf("B\n");
10 } else if (score >= 70) {
11 printf("C\n");
12 } else {
13 printf("Keep practicing\n");
14 }
15 return 0;
16}B
The conditions are checked in order, and only the first true branch runs. That is why score >= 80 does not also need score < 90: if the score were 90 or more, the first branch would already have run. Try changing score and running it again.
What counts as true
C has no special boolean type in its core: zero is false, and any non-zero value is true. Comparisons produce 1 or 0. Since C99 you can #include <stdbool.h> to get bool, true and false, which make intent clearer - use them.
Combining conditions
a && bis true when both are true.a || bis true when at least one is true.!aflips true and false.
Both && and || short-circuit: they stop as soon as the answer is known. In count > 0 && total / count > 10, the division never happens when count is 0 - a handy way to guard against dividing by zero.
1#include <stdio.h>
2#include <stdbool.h>
3
4int main(void) {
5 int age = 15;
6 bool with_adult = true;
7
8 if (age >= 18 || with_adult) {
9 printf("Entry allowed\n");
10 }
11 if (age >= 13 && age <= 19) {
12 printf("Teen ticket\n");
13 }
14 return 0;
15}Entry allowed Teen ticket
switch
When you compare one integer (or char) against many fixed values, switch is cleaner than a long if chain:
1#include <stdio.h>
2
3int main(void) {
4 char choice = 's';
5
6 switch (choice) {
7 case 'n':
8 printf("New game\n");
9 break;
10 case 'l':
11 printf("Load game\n");
12 break;
13 case 's':
14 printf("Settings\n");
15 break;
16 default:
17 printf("Unknown option\n");
18 break;
19 }
20 return 0;
21}Settings
Each case label marks where execution starts. Without break, it keeps going into the next case - called fall-through. Occasionally that is what you want (several cases sharing one block), but forgetting a break is a very common bug. default runs when no case matches.
Key takeaways
if/else if/elsechecks conditions in order and runs only the first true branch.Zero is false, anything else is true.
&&and||short-circuit. Write ranges asx > 0 && x < 10.switchjumps to a matchingcase; end each case withbreakunless you truly want fall-through.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write real C
These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.
FizzBuzz for one number
Read one integer. Print FizzBuzz if it is divisible by both 3 and 5, Fizz if only by 3, Buzz if only by 5, and otherwise the number itself.
- 15 prints FizzBuzz
- 9 prints Fizz
- 10 prints Buzz
- 7 prints 7
Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.
Tiny calculator
Read an expression like 12 * 3 - a number, an operator character, and a number - and print the result. Support +, -, * and / using a switch. For division by zero, print Error: division by zero. For any other operator, print Error: unknown operator.
Read it with scanf("%d %c %d", &a, &op, &b). Integer division is fine.
- 12 * 3
- 20 / 6
- 5 / 0
- 2 ^ 3
Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Sign in to ask questions, help other learners, and earn XP for taking part.
Loading posts…