Intuition The big picture
A program is a list of instructions. By default the CPU executes them top to bottom, one after another . Control flow statements are the only way to break that straight line: they let you choose (decide which path), repeat (loop), and jump (skip ahead/back). Every algorithm you'll ever write is built from just these three powers: sequence, selection, and iteration .
Without branching, a program could only do one fixed thing forever. Real problems need decisions ("if the user is logged in...") and repetition ("for each item in the list..."). A computer is fast precisely because it can repeat a tiny instruction billions of times — but you need a loop to express that compactly instead of writing the line a billion times.
An if statement runs a block only when a condition is true . In C, a condition is "true" when its value is non-zero , and "false" when it is exactly 0 . There is no separate boolean type required (though _Bool/<stdbool.h> exists).
if (condition) {
// runs when condition != 0
} else if (other) {
// runs when other != 0
} else {
// fallback
}
Worked example Classify a number
int n = - 3 ;
if (n > 0 ) printf ( "positive" );
else if (n == 0 ) printf ( "zero" );
else printf ( "negative" ); // prints this
Why this step? Conditions are checked in order, top to bottom ; the first true one wins and the rest are skipped. Since n>0 is false and n==0 is false, we reach else.
= vs ==
Writing if (x = 5) feels right because in math = means equality. But in C, = is assignment : x = 5 stores 5 in x and the expression's value is 5 (non-zero → always true!). The fix: use == for comparison. Trick: write if (5 == x) so a typo 5 = x becomes a compiler error.
switch selects among many cases by comparing an integer (or char) expression against constant case labels. Execution jumps to the matching label and then falls through to following statements until a break (or the end) is hit.
switch (expr) {
case 1 : /* ... */ break ;
case 2 : /* ... */ break ;
default : /* none matched */
}
Intuition Why fall-through?
switch is really a computed jump into a block, not a set of separate boxes. Once you land on a label, code keeps running downward. That's why you need break to stop — otherwise you "fall through" into the next case. This is occasionally useful (grouping cases) but a frequent bug source.
Worked example Grouping cases on purpose
switch (grade) {
case 'A' :
case 'B' : printf ( "Pass with distinction" ); break ;
case 'C' : printf ( "Pass" ); break ;
default : printf ( "Fail" );
}
Why this step? 'A' has no break, so it deliberately falls through into 'B''s code — both print the same message.
Common mistake Forgetting break
Leaving out break feels fine if you think each case is isolated like an if. The fix: add break to every case unless you intend fall-through (and comment it: /* fall through */).
Intuition Anatomy of every loop
Every loop has 4 parts: (1) initialization (set up a counter), (2) condition (when to keep going), (3) body (the work), (4) update (move toward stopping). If the condition never becomes false, you get an infinite loop .
Checks the condition before each pass. If false at the start, the body runs zero times.
int i = 0 ; // 1 init
while (i < 3 ) { // 2 condition (checked first)
printf ( " %d " , i); // 3 body
i ++ ; // 4 update
} // prints: 0 1 2
Runs the body once first , then checks the condition. Guarantees at least one execution. Note the required semicolon after while(...).
int i = 5 ;
do {
printf ( " %d " , i); // prints 5 once even though 5<3 is false
i ++ ;
} while (i < 3 );
Intuition When do-while wins
Use it for "do, then ask whether to repeat": menus, input validation ("ask for a number, then keep asking if it was invalid").
Packs init, condition, and update into one header. Equivalent to a while with the update built in.
for ( int i = 0 ; i < 3 ; i ++ ) {
printf ( " %d " , i);
}
Definition break / continue
break exits the innermost loop or switch immediately.
continue skips the rest of the current iteration and jumps to the loop's update/condition.
Worked example break vs continue
for ( int i = 0 ; i < 5 ; i ++ ) {
if (i == 2 ) continue ; // skip printing 2
if (i == 4 ) break ; // stop entirely before 4
printf ( " %d " , i);
} // prints: 0 1 3
Why this step? At i==2, continue jumps to i++ (so 2 is never printed). At i==4, break leaves the loop, so 4 never prints — and we already printed 3 in the previous iteration.
Common mistake continue in for vs while
People assume continue always behaves the same. In a for loop, continue still runs the update i++. But if you hand-translate a for into a while and put continue before the manual i++, you skip the increment → infinite loop! Fix: in while, do the update before any continue, or restructure.
goto label; jumps unconditionally to a label: in the same function . Powerful but dangerous ("spaghetti code"). The one widely-accepted use is breaking out of nested loops or centralized cleanup/error handling in C.
Worked example Legit goto — breaking nested loops
for ( int i = 0 ; i < N; i ++ )
for ( int j = 0 ; j < M; j ++ )
if ( grid [i][j] == target)
goto found; // break only exits inner loop; goto escapes both
found:
printf ( "done" );
Why this step? A single break only exits the inner j loop. goto jumps clean out of both at once.
Common mistake goto everywhere
Goto feels convenient because it's a direct jump. But unrestricted jumps make code impossible to follow. Fix: prefer loops + functions; reserve goto for nested-loop exits and error cleanup only.
S equence, S election, I teration are the 3 building blocks. The jump trio is BCG : B reak (leave), C ontinue (skip ahead), G oto (teleport). And remember do-while = "do first, ask later."
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.
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
C Operators — ==, =, &&, ||, ! produce the conditions branches test.
Loops and Time Complexity — nested loops → O ( n 2 ) O(n^2) O ( n 2 ) etc.
Functions in C — return is another control-flow jump; goto is function-local.
Boolean Logic — short-circuit evaluation of &&/|| in conditions.
Recursion — an alternative to iteration for repetition.
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.