Conditional Statements
Control flow determines which code executes based on conditions. C uses if/else for branching, switch for multi-way selection, and the ternary operator for inline conditions.
35 min•By Priygop Team•Last updated: Feb 2026
Control Flow Code
Example
#include <stdio.h>
int main() {
// if / else if / else
int score = 85;
char grade;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
printf("Score: %d, Grade: %c\n", score, grade);
// switch statement (integer/char values only)
char command;
printf("Enter command (q/h/r): ");
scanf(" %c", &command);
switch (command) {
case 'q':
printf("Quitting...\n");
break; // Without break, falls through to next case!
case 'h':
printf("Help menu\n");
break;
case 'r':
printf("Running...\n");
break;
default:
printf("Unknown command: %c\n", command);
}
// Fall-through (intentional)
int day = 3;
switch (day) {
case 1: case 2: case 3: case 4: case 5:
printf("Weekday\n");
break;
case 6: case 7:
printf("Weekend\n");
break;
}
// Nested conditions
int age = 25;
int hasLicense = 1;
if (age >= 16) {
if (hasLicense) {
printf("Can drive\n");
} else {
printf("Need a license\n");
}
} else {
printf("Too young to drive\n");
}
// Common C pitfall: = vs ==
// if (x = 5) ← ASSIGNMENT, always true!
// if (x == 5) ← COMPARISON, correct
// Tip: write constants on left: if (5 == x) ← compiler catches if (5 = x)
return 0;
}