YOUR C LEARNING COMPANION

Kahan se padhein?

Learning path
C ki buniyadChapter 12 of 73

Conditions, choices aur loops

Execution ka path selection, repetition aur control transfer samjhein; boundaries aur progress par dhyan dein.

7 min readFoundationsC17 baseline
Offline reading
Is chapter mein10 sections
0% through this chapter

Program ko aksar agla step choose karna ya kuch work repeat karna hota hai. Conditions aur loops yeh choices express karte hain. Chhote examples ko step by step follow karenge, taaki first test, repeated work aur stopping point clear rahein.

1. Condition current state ka test hai

Execution if tak pahunchne par scalar condition test hoti hai. Result nonzero ho toh controlled statement run hoti hai. Zero false hai; negative integers true hain. Test execution ke us point par hota hai; woh continuously changes watch nahi karta.

C17 · illustrative fragment
int age = 17;
int eligible;
if (age >= 18) {
    eligible = 1;
} else {
    eligible = 0;
}

Dono branches mein se exactly one value supply karti hai. Example afterward use se pehle har path par eligible ko value deta hai. C mein else optional hai, lekin uske bina false path par kya hoga, yeh reason karna hoga. Grammar else ko nearest eligible unmatched if se attach karti hai; braces intended structure clear karti hain.

2. While body se pehle test karta hai

C17 · illustrative fragment
int count = 0;
while (count < 3) {
    count += 1;
}

while test har possible repetition se pehle hota hai. Yahan body counts zero, one aur two par run hoti hai. Third update ke baad count three hai aur next test false hai. First test hi false ho toh body zero times run hogi.

Finish hone wale loop mein false condition ya doosre exit ki taraf progress identify karein. Update bhoolna specification se radically different behavior de sakta hai. Events serve karte rehne wale loop mein event aur resource rules identify karein. Sirf “meri machine par rukta hai” loop ka justification nahi.

3. For related loop controls ko saath rakhta hai

C17 · illustrative fragment
int sum = 0;
for (int value = 1; value <= 3; ++value) {
    sum += value;
}

for ko order mein padhein: initialise, test, body run, update, phir test. Yahan body one, two aur three add karke six deti hai. Header mein declared loop variable loop ke andar visible hai. Loop ke baad unrelated code use nahi kar sakta.

Parentheses ke andar two semicolons control parts separate karti hain. Closing parenthesis ke immediately baad semicolon alag hai: woh null statement ban kar poori loop body ho sakti hai. Baad ka brace block automatically repeat nahi hota. Yeh valid syntax ke saath unintended meaning ho sakta hai; indentation alone protection nahi.

4. Do-while first execution ke baad check karta hai

C17 · illustrative fragment
int count = 3;
do {
    count += 1;
} while (count < 3);

Control normally is do statement mein enter kare toh body first test se pehle run hoti hai. Count four hota hai, phir condition false. Jab repeat decide karne se pehle meaningful work ho, yeh form useful hai. Iska trailing semicolon syntax ka part hai, upar wali accidental empty loop body nahi.

5. Break aur continue ke destinations alag hain

break nearest enclosing loop ya switch se bahar jata hai. continue loop ke continuation step par jata hai. for mein next test se pehle update hota hai. while aur do-while mein test par jaata hai. Dono automatically saare surrounding constructs se bahar nahi nikalte.

C17 · illustrative fragment
int sum = 0;
for (int value = 1; value <= 3; ++value) {
    if (value == 2) {
        continue;
    }
    sum += value;
}

Second iteration addition skip karti hai, for update nahi. One aur three add hote hain, result four. Nested control flow mein pehle actual enclosing construct pehchanein. Loop ke andar nested switch ka break us switch ko break karta hai, outer loop ko nahi.

return current function se leave karta hai, return type ke according result supply karte hue ya bina value ke. goto scope restrictions ke under same function ke label par control transfer karta hai; doosre function mein jump nahi. Shared resource-cleanup path ek useful pattern hai; ordinary choices aur repetitions usually structured statements se clearer hain.

6. Switch ek label select karta hai, isolated box nahi

C17 · illustrative fragment
int choice = 2;
int charge = 0;
switch (choice) {
    case 1:
        charge = 10;
        break;
    case 2:
        charge = 20;
        break;
    default:
        charge = -1;
        break;
}

switch integer value se label select karta hai. case allowed constant value deta hai; default fallback hai. Execution selected case se later code mein continue kar sakti hai. Continuation intended na ho toh break ya deliberate transfer use karein. Integer promotion aur constant-expression rules phir bhi apply hote hain.

7. Boundary aur invariant saaf karein

Invariant aisa fact hai jo computation ke chosen points par true rakha jata hai. Sum loop mein value k process karne se just pehle earlier values sum mein add ho chuki hain. Start aur har iteration ke baad fact check karein. Sirf comment likhna proof nahi hai.

First aur last iteration, zero iterations aur arithmetic ke type-range cross karne ka possibility check karein. < ko <= karna repetition badha ya representable bound ke past increment karwa sakta hai. Familiar loop header har input ke safe body ka proof nahi.

Thoda sochkar dekhein

Continue example mein value two par addition skip hone se kya value two par stuck ho jaata hai?

Explanation padhein

Nahi. for mein continue next test se pehle update expression tak le jaata hai. Variable advance hota hai aur final sum four hai.

Primary references aur aage ki reading

Aage badhne se pehle

  • Conditions us point par examined state se path choose karti hain.
  • Loop trace mein test, body, update aur exit dikhne chahiye.
  • break, continue aur return different enclosing constructs ke rules se control transfer karte hain.
In ideas ko alag rakhein
break / continue
break nearest enclosing loop ya switch chhodta hai; continue enclosing loop ke continuation step par jata hai.
Apne words mein samjhaayein

Loop zero, one ya one-too-many times chale, yeh check karne ke liye kaunsi state trace karenge?

Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.

Apni understanding check karein

Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.

while loop ki body zero baar kyun chal sakti hai?
Har choice ki explanation padhein
  1. while check se pehle hamesha ek baar chalta hai.

    Yeh while ka rule nahi; body mein enter karne se pehle condition check hoti hai.

  2. Compiler har loop remove kar deta hai.

    Loop behavior program semantics follow karta hai; universal removal rule nahi.

  3. Pehle check par controlling expression false ho sakti hai.

    ✓ Sahi reasoning. Initially false condition pehli body execution ko rok deti hai.

C17

Pehle edition ka quick reference

Yeh compact reference existing links aur saved passages ke liye retained hai. Full context ke liye upar ki expanded lesson padhein.

Conditional Branching

if/else scalar conditions handle karta hai; switch integer controlling expression (enumerated types bhi) se label select karta hai. Selected case ke baad execution later labels se continue hota hai jab tak break, return ya goto jaisa control transfer na ho. Last case mein redundant break required nahi. Intentional fall-through document karein; C23 ka [[fallthrough]] annotation usko document karta hai, khud jump ya execution stop nahi karta.

Loops

  • for (init; condition; update) supplied initialization, continuation test aur update ko group karta hai. Initialization expression ya declaration ho sakti hai; counted iteration mein yeh form useful hai.
  • while (condition) har iteration se pehle apni condition check karta hai, isliye body zero times bhi run ho sakti hai.
  • do { ... } while (condition); har iteration ke baad apni condition check karta hai, isliye guarantee hoti hai ki body kam se kam ek baar run ho.

Jump Statements

  • break nearest enclosing loop ya switch se exit karta hai.
  • continue nearest enclosing loop ko continue karta hai. for mein condition dobara test karne se pehle update expression run hota hai; while aur do-while mein control condition test par jaata hai.
  • goto label; scope restrictions ke under same function ke label par control transfer karta hai. Error ke baad resources release karne ka shared cleanup path ek common use hai. Yeh ek useful pattern hai, goto ka only possible use nahi. Intended flow clear rakhne wale control structures prefer karein.

Agla idea padhne ke liye tayyar?

Mark this chapter as read whenever you feel ready.

CHAPTER 12 / 73

Is chapter mein

Conditions, choices aur loops

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping