Control Flow in C
Learn how to direct program execution in C with if/else conditionals, switch statements, for and while loops, and loop control using break and continue
Control flow is what turns a list of statements into a program that makes decisions and repeats work. As a procedural, imperative language, C gives you a compact but complete set of control structures: if/else for branching, switch for multi-way dispatch, and for, while, and do/while for looping. These constructs map almost directly onto the machine instructions the compiler generates, which is part of why C remains the language of operating systems and embedded firmware.
One detail shapes everything about control flow in C: there is no dedicated boolean type at the language’s core. Instead, C treats any non-zero value as true and zero as false. A condition like if (count) is perfectly valid and is true whenever count is not zero. This is a direct consequence of C’s static, weak typing — integers and conditions are interchangeable, which is powerful but demands care.
In this tutorial you will learn how to branch with if/else if/else, dispatch with switch, iterate with all three loop forms, and steer loops using break, continue, and the ternary ?: operator. Every example compiles cleanly with gcc:14 and is ready to run in Docker.
Conditionals: if, else if, and else
The if statement evaluates an expression and runs a block when it is non-zero. Chaining with else if lets you test several conditions in order, and a final else handles everything that falls through.
Create a file named conditionals.c:
| |
With score equal to 78, the first two conditions are false and score >= 70 is true, so the program prints Grade: C. Because remaining is 0 (false), the else branch runs. The ternary operator chooses the second string since -5 >= 0 is false.
The switch statement
When you need to branch on the discrete value of an integer or character, switch is clearer than a long if/else if chain. Each case is a label; execution jumps to the matching label and then falls through to the next case unless you write break. This fall-through behavior is a classic C gotcha — and occasionally a useful feature, as the grouped vowel cases below show.
Create a file named switch_demo.c:
| |
The first switch matches case 'B' and prints Good, then break exits. In the second switch, 'e' matches the empty case 'e', which falls through to the shared printf, reporting that it is a vowel.
Loops: for, while, and do-while
C offers three loop forms. The for loop bundles initialization, condition, and update into one header — ideal for counting. The while loop tests its condition before each iteration. The do/while loop tests after the body, so it always runs at least once.
Create a file named loops.c:
| |
The for loop accumulates 1+2+3+4+5 = 15. The while loop prints the countdown from 3 down to 1, stopping when count reaches 0. The do/while loop runs once, increments attempts to 1, and runs again to make attempts equal to 2 before the condition attempts < 2 becomes false.
Loop control with break and continue
break exits the nearest enclosing loop immediately, while continue skips the rest of the current iteration and jumps to the loop’s next step. They give you fine-grained control without resorting to flags or extra variables.
Create a file named loop_control.c:
| |
The first loop uses continue to skip every even i, leaving only the odd numbers 1, 3, 5, 7, and 9. The second loop walks upward from 20 and breaks the instant it hits 21, the first multiple of 7 in range.
Running with Docker
Compile and run each example inside the official GCC image — no local toolchain required.
| |
Expected Output
Running conditionals:
Grade: C
Nothing left to process
-5 is negative
Running switch_demo:
Good
'e' is a vowel
Running loops:
Sum 1..5 = 15
Countdown: 3
Countdown: 2
Countdown: 1
Attempt 1
Attempt 2
Running loop_control:
Odd numbers: 1 3 5 7 9
First multiple of 7 at or above 20: 21
Key Concepts
- Truth is numeric — C has no built-in boolean; any non-zero value is true and zero is false, so
if (count)is idiomatic. (C99’s<stdbool.h>addsbool,true, andfalseas conveniences.) else ifis just nesting — C has noelifkeyword; chained conditions are anifinside the previouselse, tested top to bottom until one matches.switchfalls through — without abreak, execution continues into the nextcase. Forgettingbreakis a common bug, but deliberate fall-through can group cases neatly.- Three loops, three timings —
foris best for counting,whiletests before the body, anddo/whileguarantees at least one execution by testing after. breakandcontinuesteer loops —breakleaves the loop entirely;continueskips to the next iteration. Both act on the nearest enclosing loop only.- The ternary
?:is an expression — unlikeif, it yields a value, making it handy for concise assignments likemax = (a > b) ? a : b. - Braces prevent surprises — always brace your conditional and loop bodies; a single unbraced statement is legal but a frequent source of bugs when code is later edited.
Comments
Loading comments...
Leave a Comment