5.1.6C Programming

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

2,120 words10 min readdifficulty · medium6 backlinks

WHY control flow exists


Selection: deciding which path to take

if / else

if (condition) {
    // runs when condition != 0
} else if (other) {
    // runs when other != 0
} else {
    // fallback
}

switch

switch (expr) {
    case 1:  /* ... */ break;
    case 2:  /* ... */ break;
    default: /* none matched */
}

Iteration: repeating work

while — test first

int i = 0;            // 1 init
while (i < 3) {       // 2 condition (checked first)
    printf("%d ", i); // 3 body
    i++;              // 4 update
}                     // prints: 0 1 2

do-while — test after

int i = 5;
do {
    printf("%d ", i);   // prints 5 once even though 5<3 is false
    i++;
} while (i < 3);

for — compact counting loop

for (int i = 0; i < 3; i++) {
    printf("%d ", i);
}
Figure — Control flow — if - else, switch, while, do-while, for, break, continue, goto

Jumps: break, continue, goto


Mnemonic


Feynman check

Recall Explain to a 12-year-old

Imagine following a recipe. if/else = "if the dough is sticky, add flour; otherwise bake it." switch = a vending machine: press a button (A1, A2...) and you go straight to that snack. while = "keep stirring while it's lumpy." do-while = "stir once, then keep stirring if still lumpy." for = "stir exactly 10 times." break = "stop stirring NOW." continue = "skip this one stir and go to the next." goto = a magic trapdoor that teleports you to another marked spot in the recipe — handy rarely, confusing usually.


Active recall

In C, a condition is "true" when its value is what?
Any non-zero value (false only when exactly 0).
What does if (x = 5) actually do?
Assigns 5 to x; the expression value is 5 (non-zero) → always true. Bug.
Why does switch need break in each case?
Execution falls through to the next case's code until a break or the block end.
What can a switch expression be?
An integer or char (integral) type compared to constant case labels.
Difference between while and do-while?
while tests before (may run 0 times); do-while tests after (runs at least once).
Give the for≡while equivalence.
for(A;B;C){S}A; while(B){ S; C; }
What does break do?
Immediately exits the innermost enclosing loop or switch.
What does continue do?
Skips the rest of the current iteration and goes to the loop's update/condition test.
Why can continue cause infinite loops in a hand-written while loop?
It can skip the increment statement if the update sits after the continue.
What is the main legitimate use of goto in C?
Breaking out of deeply nested loops or centralized error/cleanup handling.
What does break do inside two nested loops?
Exits only the innermost loop, not both.
Output of for(i=0;i<5;i++){if(i==2)continue; if(i==4)break; printf("%d",i);}?
0 1 3

Connections

  • C Operators==, =, &&, ||, ! produce the conditions branches test.
  • Loops and Time Complexity — nested loops → O(n2)O(n^2) etc.
  • Functions in Creturn is another control-flow jump; goto is function-local.
  • Boolean Logic — short-circuit evaluation of &&/|| in conditions.
  • Recursion — an alternative to iteration for repetition.

Concept Map

three powers

three powers

three powers

two-way

multi-way

true means

common bug

matches

needs to stop

without break

loop kinds

includes

includes

exits

skips iteration

Control Flow

Selection

Iteration

Jump

if - else

switch

non-zero value

= vs ==

integer or char

break

fall-through

while, do-while, for

continue

goto

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek C program by default upar se neeche, line by line chalta hai. Control flow ka kaam hai is seedhi line ko todna — yaani decision lena aur repeat karna. Teen basic powers hain: sequence (kramshः), selection (if/else, switch), aur iteration (loops). Yaad rakho C mein condition "true" tab hoti hai jab uski value zero nahi hoti; sirf 0 ko false maana jaata hai.

if/else ka matlab — "agar yeh sach hai to yeh karo, warna woh." Ek bahut bada beginner trap: == (compare) aur = (assign) ko confuse karna. if(x = 5) actually x mein 5 daal deta hai aur hamesha true ho jaata hai — bug! switch ka funda yeh hai ki matching case pe jump hota hai aur fir fall-through hota hai, isliye har case ke baad break lagana zaroori hai, warna neeche wale cases bhi chal jaayenge.

Loops mein simple rule: while pehle condition check karta hai (0 baar bhi chal sakta hai), do-while pehle body chalata hai fir check karta hai (kam se kam 1 baar chalega — menu ya input validation ke liye perfect). for to bas init, condition, update ko ek line mein pack kar deta hai; isiliye for(A;B;C){S} exactly A; while(B){S; C;} ke barabar hai.

Jump statements: break matlab loop se turant bahar nikal jao, continue matlab iss iteration ka baaki part chhodo aur agle round pe jao. Ek trap — continue for loop mein i++ chalata hai, par agar tum while mein khud i++ likhte ho aur continue usse pehle aa gaya, to infinite loop! goto ek teleport hai — sirf nested loops se ekdum bahar nikalne ya error cleanup ke liye use karo, warna code spaghetti ban jaata hai.

Go deeper — visual, from zero

Test yourself — C Programming

Connections