Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Control flow is the backbone of every C program you'll write. Without it, your code would execute line by line from top to bottom with no ability to make decisions, repeat tasks, or respond to different inputs. When you're tested on control flow, you're really being tested on your understanding of conditional logic, iteration patterns, and program execution order—concepts that appear in virtually every programming problem you'll encounter.
The key insight is this: control flow statements fall into distinct categories based on what they do—branching (choosing a path), looping (repeating code), and jumping (moving execution elsewhere). Don't just memorize syntax; know which statement solves which problem and why you'd choose one over another. That's what separates students who can write working code from those who just recognize keywords.
These statements let your program choose different execution paths based on conditions. The core mechanism is Boolean evaluation—every condition resolves to either true (non-zero) or false (zero).
if (condition) { /* code */ }—parentheses around the condition are required, braces optional for single statements but always recommendedif is prerequisite to mastering every other control structureif (condition) { /* true path */ } else { /* false path */ }—exactly one block will always executeelse if for multiple conditions, though switch is often cleaner for many discrete valuesbreak; this is both a feature and a common bug sourceint, char, and enums, but not with strings or floating-point valuesswitch (grade) { case 'A': printf("Excellent"); break; case 'B': printf("Good"); break; default: printf("Keep trying"); }
Compare: if-else chains vs. switch—both handle multiple conditions, but switch is cleaner and potentially faster when comparing one variable against many constant values. Use if-else when conditions involve ranges, comparisons, or non-constant expressions.
Loops execute a block of code multiple times. The key distinction is when the condition is checked and whether the number of iterations is known in advance.
for (init; condition; update)for (;;) creates an infinite loop, and any part can be omitted or contain multiple expressionsfor (int i = 0; i < 10; i++) { printf("%d ", i); }
while (condition) { /* body */ }—you must update the condition variable inside the loop or risk infinite loopsdo { /* body */ } while (condition);—note the semicolon after the closing parenthesis (easy to forget!)Compare: while vs. do-while—both are condition-controlled loops, but while may skip the body entirely while do-while always runs once. If an exam asks about "guaranteed execution," do-while is your answer.
Compare: for vs. while—functionally equivalent (any for can be rewritten as while), but for is conventional when the iteration count is known, while when it depends on runtime conditions.
These statements transfer control to another point in the program. They interrupt normal sequential execution and should be used deliberately.
for, while, do-while, or switch statementbreak, execution "falls through" to subsequent cases unintentionallyfor loops, the update expression still executes; in while/do-while, control goes directly to the conditionif statementsfor (int i = 0; i < 10; i++) { if (i % 2 == 0) continue; // Skip even numbers printf("%d ", i); // Prints: 1 3 5 7 9 }
Compare: break vs. continue—break exits the loop entirely, continue skips to the next iteration. Both affect only the innermost loop, and both are commonly tested in trace-the-output questions.
break statements would be awkwardreturn value; sends a result to the calling code; return; exits void functions without a valueCompare: break vs. return—break exits a loop but continues the function, return exits the entire function. In main(), return also ends the program.
| Concept | Best Examples |
|---|---|
| Binary decisions | if, if-else |
| Multi-way branching | switch, else if chains |
| Count-controlled loops | for |
| Condition-controlled loops | while, do-while |
| Guaranteed single execution | do-while |
| Early loop termination | break |
| Skip iteration | continue |
| Function exit | return |
| Unconditional jump (avoid) | goto |
What's the key difference between while and do-while? Write a scenario where choosing the wrong one would cause a bug.
If you need to check a variable against the values 1, 2, 3, 4, and 5, which control structure would be most appropriate—if-else chain or switch? Why?
Compare break and continue: what happens to loop execution after each? In a for loop, does the update expression run after continue?
Convert this for loop to an equivalent while loop:
for (int i = 0; i < n; i++) { printf("%d", i); }
A function needs to validate user input, prompting repeatedly until valid data is entered. Which loop type is most appropriate, and why would the alternatives be problematic?