Control Flow in C
Control flow determines execution order. C provides if/else, switch, and three loop forms — all driven by integer expressions.
If / Else
#include <stdio.h>
int main(void) {
int score = 85;
if (score >= 90)
printf("A\n");
else if (score >= 80)
printf("B\n");
else
printf("C or below\n");
return 0;
}
Always brace multi-line blocks. Single statements may omit braces but this causes bugs during maintenance.
Switch
switch (command) {
case 'q':
case 'Q':
quit = 1;
break; /* essential — fall-through otherwise */
case 'h':
show_help();
break;
default:
fprintf(stderr, "Unknown: %c\n", command);
}
C23 adds case 1 ... 5: range labels. Use default to catch unexpected values.
For Loop
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
/* C99: declare loop variable in for-init */
int arr[] = {3, 1, 4, 1, 5};
for (size_t i = 0; i < sizeof arr / sizeof arr[0]; i++) {
printf("%d ", arr[i]);
}
While and Do-While
int n = 10;
while (n > 0) {
printf("%d ", n);
n--;
}
int ch;
do {
ch = getchar();
putchar(ch);
} while (ch != '\n'); /* runs at least once */
Break, Continue, Goto
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) continue; /* skip evens */
if (i > 20) break; /* exit loop */
printf("%d ", i);
}
/* goto: acceptable for centralized cleanup in C */
if (error) goto cleanup;
cleanup:
free(buffer);
Common Pitfalls
- Treating compiler warnings as optional rather than actionable feedback.
- Skipping error checks on library and system calls.
- Copy-pasting examples without adapting to your project’s conventions.
Best Practices
- Enable strict compiler warnings and fix them before merging.
- Write small, testable units with clear input/output contracts.
- Document non-obvious invariants and preconditions.
- Use version control and code review for every change.
Memory and Performance Notes
Branch prediction affects loop performance. Prefer predictable branches in hot loops.
Exercise
Print all prime numbers between 2 and 100 using nested loops. Count how many primes you found.
Hint: A number is prime if no integer from 2 to sqrt(n) divides it evenly.
Summary
Apply these concepts in small programs before moving to larger projects. Combine with adjacent topics in the learning path for deeper mastery.
Real-World Application
These concepts appear in production codebases — from operating system kernels to embedded firmware. Study open-source projects that use this topic extensively to see idiomatic patterns at scale.
Debugging Checklist
- Reproduce the issue with the smallest possible input.
- Enable compiler warnings and sanitizers.
- Use a debugger to inspect state at the failure point.
- Verify assumptions about types, sizes, and return values.
- Compare working and broken code paths side by side.
- Write a regression test once the bug is fixed.
Further Reading
Consult the ISO C standard, Effective C by Robert C. Seacord, and your compiler documentation for platform-specific behavior.
Quick Reference
Review the code examples on this page before starting the exercise. Type them manually to build muscle memory.