Lesson 47 min read

Conditionals

Traffic lights for your code β€” red means stop, green means go

Traffic Lights for Your Code

Without conditionals, your program would be a single straight road β€” no turns, no choices, no decisions. Conditionals are the traffic lights that let your code take different paths depending on what's happening.

But before we dive in, you need to understand one critical thing about C: there is no true boolean type (at least not natively). In C, truth is a number:

  • 0 is false β€” zero, nada, nothing.
  • Everything else is true β€” 1, -5, 42, 999 β€” all truthy.

This simplicity is elegant, but it opens the door to some sneaky bugs.

Truthiness in C

#include <stdio.h>
int main() {
if (1) printf("1 is true\n");
if (-1) printf("-1 is true\n");
if (42) printf("42 is true\n");
if (0) printf("0 is true\n"); // This never prints!
// Common pattern: checking if a value exists
int items_in_cart = 3;
if (items_in_cart) {
printf("You have %d items\n", items_in_cart);
} else {
printf("Your cart is empty\n");
}
return 0;
}
Output
1 is true
-1 is true
42 is true
You have 3 items

if, else if, else

The classic decision structure. C evaluates each condition top-to-bottom and takes the first branch that's true. If none match, the else block runs (if you have one).

Grade Calculator

#include <stdio.h>
int main() {
int score = 78;
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else if (score >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}
printf("Score: %d\n", score);
return 0;
}
Output
Grade: C
Score: 78

switch/case β€” The Vending Machine

When you're comparing one variable against many specific values, switch is cleaner than a chain of if/else if. Think of it like a vending machine β€” you press a button (the case) and get a specific result.

But there's a trap: you must use break after each case. Without it, execution "falls through" into the next case like a ball rolling down stairs.

switch/case β€” Menu System

#include <stdio.h>
int main() {
int choice = 2;
printf("=== MENU ===\n");
printf("1. New Game\n");
printf("2. Load Game\n");
printf("3. Settings\n");
printf("4. Quit\n\n");
switch (choice) {
case 1:
printf("Starting new game...\n");
break;
case 2:
printf("Loading saved game...\n");
break;
case 3:
printf("Opening settings...\n");
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
return 0;
}
Output
=== MENU ===
1. New Game
2. Load Game
3. Settings
4. Quit

Loading saved game...

Fall-Through β€” Bug or Feature?

#include <stdio.h>
int main() {
// Intentional fall-through: grouping cases
char grade = 'B';
switch (grade) {
case 'A':
case 'B':
printf("Great job!\n");
break;
case 'C':
printf("Not bad, keep going.\n");
break;
case 'D':
case 'F':
printf("Need improvement.\n");
break;
default:
printf("Invalid grade.\n");
}
return 0;
}
Output
Great job!
Note: The two most common conditional bugs in C:
1. = vs == β€” Writing if (x = 5) instead of if (x == 5). The first assigns 5 to x (and is always true since 5 is nonzero). The compiler might not warn you. Some programmers write if (5 == x) to catch this β€” a typo 5 = x won't compile.
2. Forgetting break in switch β€” Without break, execution falls through to the next case. Sometimes intentional, usually a bug.

The Ternary Operator

The ternary operator ? : is a one-line if/else. It's great for simple assignments but don't overuse it β€” nested ternaries become unreadable fast.

Ternary Operator

#include <stdio.h>
int main() {
int a = 15, b = 23;
// Instead of a 5-line if/else:
int max = (a > b) ? a : b;
int min = (a < b) ? a : b;
printf("Max: %d\n", max);
printf("Min: %d\n", min);
// Inline in printf
int temp = -3;
printf("%d is %s\n", temp, (temp >= 0) ? "positive" : "negative");
// Absolute value
int abs_val = (temp < 0) ? -temp : temp;
printf("Absolute value: %d\n", abs_val);
return 0;
}
Output
Max: 23
Min: 15
-3 is negative
Absolute value: 3
Challenge

Quick check

What does the expression if (x = 0) do in C?
← Input & OutputLoops β†’