5.1.6 · HinglishC Programming

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

1,998 words9 min readRead in English

5.1.6 · Coding › C Programming


Control flow kyun exist karta hai


Selection: kaunsa path lena hai yeh decide karna

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: kaam repeat karna

while — pehle test karo

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 — baad mein test karo

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 Ek 12-saal ke bachche ko samjhao

Socho tum ek recipe follow kar rahe ho. if/else = "agar dough sticky hai, toh flour daalo; warna bake karo." switch = ek vending machine: ek button dabaao (A1, A2...) aur tum seedha us snack par pahunch jaate ho. while = "jab tak gaaanth hai tab tak chalate raho hilaana." do-while = "ek baar hilao, phir agar abhi bhi gaaanth hai toh hilate raho." for = "exactly 10 baar hilao." break = "ABHI hilana band karo." continue = "yeh ek hilana skip karo aur agle par jao." goto = ek magic trapdoor jo tumhe recipe mein kisi aur marked jagah teleport kar deta hai — kabhi-kabhi kaam aata hai, aksar confusing hota hai.


Active recall

C mein, ek condition "true" kab hoti hai?
Jab uski value koi bhi non-zero ho (false sirf tab hoti hai jab exactly 0 ho).
if (x = 5) actually kya karta hai?
x mein 5 assign karta hai; expression ki value 5 hoti hai (non-zero) → hamesha true. Bug.
switch mein har case mein break kyun chahiye?
Execution agle case ke code mein fall through hoti rehti hai jab tak break ya block end na aaye.
switch expression kya ho sakta hai?
Ek integer ya char (integral) type jo constant case labels se compare hota hai.
while aur do-while mein kya fark hai?
while pehle test karta hai (0 baar chal sakta hai); do-while baad mein test karta hai (kam se kam ek baar chalta hai).
for≡while equivalence do.
for(A;B;C){S}A; while(B){ S; C; }
break kya karta hai?
Innermost enclosing loop ya switch se turant exit karta hai.
continue kya karta hai?
Current iteration ka baaki hissa skip karta hai aur loop ke update/condition test par jaata hai.
Hand-written while loop mein continue infinite loops kyun cause kar sakta hai?
Yeh increment statement ko skip kar sakta hai agar update continue ke baad rakha ho.
C mein goto ka main legitimate use kya hai?
Deeply nested loops se bahar nikalna ya centralized error/cleanup handling.
Do nested loops ke andar break kya karta hai?
Sirf innermost loop se exit karta hai, dono se nahi.
for(i=0;i<5;i++){if(i==2)continue; if(i==4)break; printf("%d",i);} ka output kya hai?
0 1 3

Connections

  • C Operators==, =, &&, ||, ! woh conditions produce karte hain jo branches test karti hain.
  • Loops and Time Complexity — nested loops → etc.
  • Functions in Creturn ek aur control-flow jump hai; goto function-local hai.
  • Boolean Logic — conditions mein &&/|| ki short-circuit evaluation.
  • Recursion — repetition ke liye iteration ka ek alternative.

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