5.1.6 · D1C Programming

Foundations — Control flow — if - else, switch, while, do-while, for, break, continue, goto

3,665 words17 min readBack to topic

Before you can read a single if or for, you must own the tiny alphabet they are built from. This page defines every symbol, word, and shape the parent note leaned on — starting from absolutely nothing.


0. The picture behind ALL of it: the instruction line

Imagine your program as a vertical ladder of steps. The computer stands on step 1, does it, steps down to step 2, does it, and so on. That downward walk is called sequence — the default, and the thing every other tool modifies.

Figure — Control flow — if - else, switch, while, do-while, for, break, continue, goto

1. The semicolon ;, curly braces { }, and parentheses ( )


2. What "true" and "false" really are: the zero test

This is the single most important idea on the page. C has no special true/false switch built into its oldest core. Instead:

Figure — Control flow — if - else, switch, while, do-while, for, break, continue, goto

Look at the figure: the number line is split into just two zones — the single point 0 (false, coral) and everything else (true, mint). This is why the parent could say if (n) with no comparison at all: it just checks "is n non-zero?"

Related idea: full Boolean Logic (AND, OR, NOT) sits on top of this zero test.


3. The comparison operators — how we make a true/false number

You rarely write raw numbers as conditions. You write questions, and the questions produce 1 (true) or 0 (false).

Recall What does

3 != 3 evaluate to? 0 (false) — because 3 is equal to 3, so "not equal" is false.


4. Combining questions: &&, ||, and !

One question is rarely enough. Real decisions say "if the user is logged in and the cart is not empty." C gives you three logical operators to glue and flip questions.

Recall What is

!(3 > 5)? 3 > 5 is 0 (false); ! flips it to 1 (true).


5. The ternary ?: — an if/else that is one value

Recall What does

(2 > 9) ? 100 : 7 evaluate to? 7 — because 2 > 9 is false, so the after-colon value is chosen.


6. The if and else statements — the core choice

Now we assemble the pieces from sections 1–4 into the first real control-flow tool.

Recall If

n = 0, which branch of the if / else if / else chain above runs? The else if (n == 0) branch — the first true condition wins.


7. Increment ++, the counter's engine


8. Anatomy of a loop: four moving parts

Now we assemble the pieces into the shape that powers all repetition.

Figure — Control flow — if - else, switch, while, do-while, for, break, continue, goto

Trace the arrows in the figure: init runs once (lavender), then the loop circles through condition → body → update → back to condition. The green exit arrow fires the moment the condition's zero-test says "false". This exact diagram is the skeleton of while, for, and do-while — they only differ in when the condition is tested. Deeper timing analysis lives in Loops and Time Complexity.


9. The three loop keywords: while, do-while, for

Recall Which loop always runs its body at least once?

do-while — it tests the condition after the first pass.


10. Steering inside a loop: break and continue

Recall In

switch, what happens if you forget break? Execution falls through and runs the next case's code too, until a break or the block ends.


11. Integer and char — what switch compares


12. The switch statement, labels and the colon :

Recall Which switch label runs when no case matches?

default: — the catch-all, playing the role else plays in an if-chain.


13. The goto statement — the unconditional jump

Recall Why use

goto instead of break to leave two nested loops? break only exits the innermost loop; goto jumps clean out of both at once.


Prerequisite map

Sequence - top to bottom

Statements and blocks

Parentheses grouping

Conditions

Zero test - true is non zero

Comparison operators

Logical and or not

Ternary question colon

if else and switch

Increment plus plus

Loop four parts

while for do while

break and continue

Integer and char

Labels case default colon

goto

Control Flow

Everything on the left feeds the parent topic (Control Flow) on the right. Notice the zero test is the hub: both selection and iteration route through it.


Equipment checklist

Cover the right side and test yourself — you are ready for the parent note only if you can answer all of these.

In C, which single value counts as false?
Exactly 0; every other value is true.
What is the difference between = and ==?
= stores a value (assignment); == asks whether two values are equal.
What do parentheses ( ) do in a condition?
Group the condition into one unit that is evaluated first, before its result is tested.
When is A && B true?
Only when both A and B are true (non-zero).
When is A || B true?
When at least one of A or B is true.
What does !A do?
Flips the truth: !0 is 1, and any non-zero becomes 0.
What does cond ? x : y give you?
x if cond is true, otherwise y — a one-expression if/else.
What does an if (condition) statement do, and what does else add?
if runs its block only when the condition is non-zero; else runs its block only when the condition was zero.
What does i++ do and why does a loop need it?
Adds 1 to i; without an update the condition never turns false → infinite loop.
Name the four parts of every loop.
Init, condition, body, update.
What is the syntax difference between while and do-while?
while (cond) { } tests before the body; do { } while (cond); tests after and needs the trailing semicolon.
What are the three parts of a for header, and how are they separated?
Init; condition; update — separated by two semicolons.
What does break do?
Immediately exits the innermost loop or switch.
What does continue do?
Skips the rest of the current pass and jumps to the loop's update/condition.
What is a "block" in C?
A group of statements inside { }, treated as one unit.
Which types can a switch branch on?
Integral types — int or char (chars are small integers).
Write the skeleton of a switch statement.
switch (expr) { case A: ... break; default: ... } — jumps to the matching case, falls through until a break.
Which switch label runs when no case matches?
default: — the catch-all.
What does the goto statement do?
Jumps unconditionally to a label: in the same function — used mainly to escape nested loops or for error cleanup.
What punctuation marks a label like found: or case 1:?
A colon : (a signpost, not an action).
What does 3 != 4 evaluate to?
1 (true) — they are not equal.
Why must you write if (n) carefully — what is being tested?
Whether n is non-zero (the zero test), not whether n equals some other value.