Loops in C: for Loop (Initialization; Condition; Increment) Notes in Hindi & English : for लूप: तीनों अभिव्यक्तियाँ, नेस्टेड for लूप्स और अनंत लूप और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Loops in C: for Loop (Initialization; Condition; Increment) (for लूप: तीनों अभिव्यक्तियाँ, नेस्टेड for लूप्स और अनंत लूप) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: The for loop integrates initialization, continuation condition, and update expressions inside a three-part header (init; cond; update) ideal for counter-controlled loops. All three expressions are optional, allowing comma-separated updates, C99 loop-scoped declarations, nested matrices, and canonical for (;;) infinite loops.

अध्याय सार: for लूप तीन भागों वाले हेडर (init; cond; update) में इनिशियलाइजेशन, कंडीशन और अपडेट को एक साथ जोड़ता है, जो निश्चित गिनती वाले लूप्स के लिए उत्तम है। इसकी तीनों अभिव्यक्तियाँ वैकल्पिक हैं, जिससे मल्टी-वेरिएबल कॉमा एक्सप्रेशन, C99 लूप-स्कोप्ड डिक्लेरेशन, नेस्टेड लूप्स और for (;;) अनंत लूप संभव होते हैं।

Loops in C: for Loop (Initialization; Condition; Increment): Detailed Notes & Architecture (for लूप: तीनों अभिव्यक्तियाँ, नेस्टेड for लूप्स और अनंत लूप)

1. Execution Order of the Three Header Expressions (तीनों हेडर अभिव्यक्तियों का निष्पादन क्रम)

[EN] In 'for (init; cond; update) { body }', init executes strictly once before iteration starts. Next, cond is evaluated (making for an entry-controlled loop); if non-zero, body executes, followed by update, and control jumps back to re-test cond. If cond evaluates to 0 on the initial check, neither body nor update runs even once. Regardless of which expressions are omitted, the two separating semicolons are always syntactically mandatory.

[HI] for (init; cond; update) { body } में init केवल एक बार लूप शुरू होने से पहले चलता है। इसके बाद cond की जांच होती है; सत्य (गैर-शून्य) होने पर body चलती है, फिर update चलता है और पुनः cond जांची जाती है। यदि पहली ही जांच में cond असत्य हो तो न body चलती है न update। तीनों हिस्से खाली छोड़े जा सकते हैं, लेकिन उनके बीच के दोनों सेमीकोलन हमेशा अनिवार्य होते हैं।

2. Optional Clauses, Comma Operator & Canonical for(;;) (वैकल्पिक खंड, कॉमा ऑपरेटर व for(;;) अनंत लूप)

[EN] Omitting init reuses an existing variable, omitting update moves updates inside the body, and omitting cond causes C to treat the test as implicitly 1 (true), producing the standard infinite loop 'for (;;)'. Multiple variables can be initialized or updated together using the comma operator, such as 'for (i = 0, j = n - 1; i < j; i++, j--)'. Beware placing commas in cond ('i < 10, j > 0'): the comma operator evaluates and discards the left test, letting only the rightmost operand control termination.

[HI] init छोड़ने पर पहले से घोषित वेरिएबल उपयोग होता है, update हटाकर बॉडी में लिखा जा सकता है, और cond खाली छोड़ने पर शर्त स्वतः सत्य (1) मान ली जाती है जिससे 'for (;;)' अनंत लूप बनता है। कॉमा ऑपरेटर से एक साथ कई काउंटर जैसे 'i = 0, j = n - 1' और 'i++, j--' चलाए जा सकते हैं। परंतु शर्त हिस्से में कॉमा (i < 10, j > 0) लिखने पर बायाँ मान व्यर्थ हो जाता है और केवल सबसे दायाँ एक्सप्रेशन ही लूप रोकता है।

3. C89 vs C99 Loop-Header Scope Rules (C89 बनाम C99 हेडर वेरिएबल स्कोप नियम)

[EN] ANSI C89 forbade declaring variables inside the for header, requiring declaration beforehand above the loop. C99 lifted this restriction so 'for (int i = 0; i < n; i++)' compiles cleanly while restricting i's lifetime strictly to the loop header and body; reading i after the loop fails unless an outer variable named i exists. All variables declared in one C99 init clause must share the same base type (e.g., 'int i = 0, j = 5', not mixing int and float).

[HI] ANSI C89 में for हेडर के अंदर वेरिएबल घोषित करना वर्जित था और उसे लूप से पहले घोषित करना पड़ता था। C99 ने 'for (int i = 0; i < n; i++)' की अनुमति दी, जहाँ i का स्कोप केवल लूप तक सीमित रहता है और लूप के बाद उसे एक्सेस करने पर एरर आता है। एक ही हेडर में घोषित सभी वेरिएबल्स का मूल डेटा टाइप समान होना जरूरी है (जैसे int i = 0, j = 5)।

4. Nested for Loops, Matrices & Cache Locality (नेस्टेड for लूप्स, मैट्रिक्स व कैश लोकैलिटी)

[EN] Nesting an inner for loop inside an outer for loop multiplies their pass counts: M outer iterations by N inner iterations yields M * N total inner executions, standard in 2D matrix math and sorting. A triangular inner limit (j <= i for i = 0..n-1) runs n(n + 1)/2 times in O(n^2) complexity. Because C stores multidimensional arrays in row-major order, iterating rows in the outer loop and columns in the inner loop (A[i][j]) maximizes sequential CPU cache hits.

[HI] एक बाहरी for लूप के अंदर आंतरिक for लूप रखने से कुल चक्र गुणित हो जाते हैं (M बाहरी × N आंतरिक = M*N बार)। त्रिकोणीय सीमा (j <= i) वाले नेस्टेड लूप में आंतरिक भाग n(n + 1)/2 बार चलता है जो O(n^2) जटिलता देता है। C भाषा में 2D एरे रो-मेजर (Row-major) क्रम में संग्रहीत होते हैं, इसलिए बाहरी लूप में रो (i) और आंतरिक लूप में कॉलम (j) रखने से A[i][j] पर बेहतरीन CPU कैश प्रदर्शन मिलता है।

Syntax, Formula & Technical Example (Loops in C: for Loop (Initialization; Condition; Increment))

#include <stdio.h>
int main(void) {
    for (int i = 1, j = 5; i <= 3 && j >= 3; i++, j--) { // Multi-variable C99 header
        for (int k = 1; k <= i; k++) {
            printf("(%d,%d) ", i, j); // Triangular nested execution
        }
        printf("\n");
    }
    return 0; // Prints 1 + 2 + 3 = 6 pairs total
}

Loops in C: for Loop (Initialization; Condition; Increment) Quick Reference & Comparison Table

for Header PartStandard C Execution RuleExam Trap / Key Point
init (expr1)Runs once before the first condition testC99 allows 'int i = 0' scoped only to the loop
cond (expr2)Tested before every pass; blank equals 1 (true)Comma 'a<5, b>0' obeys only rightmost test
update (expr3)Runs after body prior to next cond checkTarget where 'continue' transfers control
for (;;) idiomEmpty clauses separated by two semicolonsCanonical infinite loop requiring break/return

Loops in C: for Loop (Initialization; Condition; Increment) Top 20 Bilingual MCQs with Answers

Q1. In 'for (E1; E2; E3) Body;', what is the exact order of evaluation when E2 passes once and fails on the second test?

('for (E1; E2; E3) Body;' में जब E2 पहली बार सत्य और दूसरी बार असत्य हो, तो निष्पादन क्रम क्या होगा?)

(A) E2 -> E1 -> Body -> E3

(B) E1 -> E2 -> Body -> E3 -> E2

(C) E1 -> Body -> E3 -> E2

(D) E1 -> E2 -> E3 -> Body -> E2

Correct Answer: (B)

[EN] Init E1 runs once first, then pre-test E2; if true, Body runs, then update E3, then re-test E2.

[HI] सबसे पहले एक बार E1 चलता है, फिर शर्त E2, उसके बाद Body, फिर अपडेट E3 और अंत में पुनः शर्त E2 जांची जाती है।

Q2. What is the minimum number of times the body of a for loop can execute?

(for लूप की बॉडी न्यूनतम कितनी बार निष्पादित हो सकती है?)

(A) 1 time

(B) Depends on the update clause

(C) 0 times

(D) 2 times

Correct Answer: (C)

[EN] Like while, for is an entry-controlled loop; if the initial condition fails immediately, the body never runs.

[HI] while की तरह for भी एंट्री-कंट्रोल्ड लूप है; प्रारंभिक शर्त असत्य होने पर बॉडी 0 बार चलती है।

Q3. Which for loop syntax creates a valid infinite loop in ISO C?

(मानक ISO C में इनमें से कौन सा सिंटैक्स वैध अनंत for लूप बनाता है?)

(A) for (;)

(B) for (;;; )

(C) for ()

(D) for (;;)

Correct Answer: (D)

[EN] A for header requires exactly two semicolons; omitting the middle condition defaults it to true (1).

[HI] for हेडर में ठीक दो सेमीकोलन अनिवार्य हैं और बीच की शर्त खाली छोड़ने पर वह स्वतः सत्य (1) मानी जाती है।

Q4. In C99/C11, what happens when running: for (int i = 0; i < 4; i++) { } printf("%d", i); (with no outer i)?

(C99/C11 में (बिना बाहरी i के) 'for (int i = 0; i < 4; i++) { } printf("%d", i);' चलाने पर क्या होगा?)

(A) Prints 3

(B) Prints 4

(C) Prints garbage value

(D) Compile-time error ('i' undeclared outside the loop)

Correct Answer: (D)

[EN] C99 restricts variables declared in the for initializer to the scope of that loop only.

[HI] C99 में for हेडर के अंदर घोषित वेरिएबल का स्कोप केवल उस लूप तक सीमित होता है, अतः बाहर उपयोग करने पर कंपाइल एरर आता है।

Q5. How many iterations occur in: for (int i = 0, j = 4; i < 10, j > 1; i++, j--) printf("X");?

('for (int i = 0, j = 4; i < 10, j > 1; i++, j--) printf("X");' में लूप कितनी बार चलेगा?)

(A) 10 times

(B) 2 times

(C) Compile-time syntax error

(D) 3 times (prints XXX)

Correct Answer: (D)

[EN] The comma operator in cond discards i < 10 and tests only j > 1, executing for j = 4, 3, and 2 (3 times).

[HI] शर्त में कॉमा ऑपरेटर केवल दाईं शर्त j > 1 को मानता है, अतः j = 4, 3 और 2 के लिए लूप 3 बार चलेगा।

Q6. How many times does '*' print in: for (int i = 0; i < 4; i++) for (int j = 0; j <= i; j++) printf("*");?

('for (int i = 0; i < 4; i++) for (int j = 0; j <= i; j++) printf("*");' में कितने '*' प्रिंट होंगे?)

(A) 6

(B) 4

(C) 16

(D) 10

Correct Answer: (D)

[EN] For i = 0,1,2,3, the inner loop runs 1, 2, 3, and 4 times respectively: 1 + 2 + 3 + 4 = 10.

[HI] i = 0, 1, 2, 3 के लिए आंतरिक लूप क्रमशः 1, 2, 3, 4 बार चलता है, जिनका योग 1 + 2 + 3 + 4 = 10 है।

Q7. What is the output of: int i; for (i = 0; i < 5; i++); printf("%d", i);?

('int i; for (i = 0; i < 5; i++); printf("%d", i);' का आउटपुट क्या होगा?)

(A) 5

(B) 0 1 2 3 4

(C) Infinite loop

(D) 4

Correct Answer: (A)

[EN] The trailing semicolon makes the loop body empty while i increments to 5; then printf runs once printing 5.

[HI] लूप के अंत में सेमीकोलन होने से खाली लूप 5 तक चलता है और लूप समाप्त होने पर printf एक बार 5 प्रिंट करता है।

Q8. What is the exact output of: int i = 0; for (printf("A"); i < 2 && printf("B"); printf("C")) i++;?

(कोड 'int i = 0; for (printf("A"); i < 2 && printf("B"); printf("C")) i++;' का सटीक आउटपुट क्या है?)

(A) ABBC

(B) ABCBC

(C) ABCB

(D) ABCABC

Correct Answer: (B)

[EN] Init prints A; test1 runs i<2 && B (prints B); body i++ (1); update1 prints C; test2 prints B; body i++ (2); update2 prints C; test3 fails i<2 short-circuiting B.

[HI] Init से A, जांच1 में B, अपडेट1 में C, जांच2 में B, अपडेट2 में C प्रिंट होता है और जांच3 में i<2 असत्य होकर B शॉर्ट-सर्किट हो जाता है: उत्तर ABCBC।

Q9. What does 'for (int i = 0; i; i++) printf("%d", i);' print?

('for (int i = 0; i; i++) printf("%d", i);' क्या प्रिंट करेगा?)

(A) Infinite loop

(B) 1

(C) 0

(D) Nothing (0 iterations)

Correct Answer: (D)

[EN] Condition 'i' starts at 0 (false), so the loop immediately exits without executing the body.

[HI] शर्त 'i' का प्रारंभिक मान 0 (असत्य) होने से लूप एक बार भी चले बिना तुरंत समाप्त हो जाता है।

Q10. Trace the output: for (int i = 1; i <= 10; i += 2) { printf("%d ", i); i++; }

('for (int i = 1; i <= 10; i += 2) { printf("%d ", i); i++; }' का आउटपुट क्या होगा?)

(A) 1 3 6 9

(B) 1 4 7 10

(C) 1 4 7

(D) 1 3 5 7 9

Correct Answer: (B)

[EN] i=1 prints 1 (body ->2, update ->4); i=4 prints 4 (body ->5, update ->7); i=7 prints 7 (body ->8, update ->10); i=10 prints 10 (body ->11, update ->13 > 10 stops).

[HI] बॉडी में i++ (1 की वृद्धि) और अपडेट में i+=2 (2 की वृद्धि) से i हर चक्र में कुल 3 बढ़ता है, अतः 1 4 7 10 प्रिंट होगा।

Q11. Why does 'for (int i = 0, float f = 1.0; i < 3; i++)' fail to compile in C?

(C में 'for (int i = 0, float f = 1.0; i < 3; i++)' कंपाइल क्यों नहीं होता?)

(A) Only pointers can be initialized in for

(B) float variables cannot appear in for loops

(C) A single declaration in the for initializer cannot mix two different base types

(D) Comma is forbidden in the init clause

Correct Answer: (C)

[EN] When declaring variables inside init, all declarators share one type specifier (e.g., int i = 0, j = 1).

[HI] for इनिशियलाइजर में एक ही डिक्लेरेशन के भीतर दो अलग-अलग मूल डेटा टाइप (int और float) एक साथ घोषित नहीं किए जा सकते।

Q12. When 'continue;' executes inside a 'for (init; cond; update)' loop, where does control go next?

(जब 'for (init; cond; update)' लूप के भीतर 'continue;' चलता है, तो कंट्रोल आगे कहाँ जाता है?)

(A) Back to the init expression

(B) Out of the entire for loop

(C) Directly to cond skipping update

(D) Directly to the update expression, then to cond

Correct Answer: (D)

[EN] Unlike while/do-while, continue inside a for loop always executes the update step before re-testing cond.

[HI] for लूप में continue चलने पर शेष बॉडी छूटती है और कंट्रोल सबसे पहले update भाग पर जाकर फिर cond जांचता है।

Q13. Why does 'for (i = 0; i < 5)' trigger a compile-time syntax error?

('for (i = 0; i < 5)' लिखने पर कंपाइल-टाइम सिंटैक्स एरर क्यों आता है?)

(A) i must start at 1

(B) The for header requires two semicolons separating three clauses even if update is empty

(C) The condition must use == instead of <

(D) for requires curly braces

Correct Answer: (B)

[EN] Writing 'for (i = 0; i < 5;)' with the second semicolon is valid, whereas omitting it is illegal.

[HI] for हेडर में तीन हिस्सों को बांटने वाले दोनों सेमीकोलन जरूरी हैं (जैसे for(i=0; i<5;))।

Q14. To sum a 2D array int A[1000][1000] efficiently in C, how should nested for loops be ordered?

(C में 2D एरे int A[1000][1000] का योग तेज गति से करने के लिए नेस्टेड for लूप्स किस क्रम में होने चाहिए?)

(A) Outer loop over row i, inner loop over column j accessing A[i][j]

(B) Reverse descending columns first

(C) Outer loop over column j, inner loop over row i accessing A[i][j]

(D) Both orders yield identical memory access

Correct Answer: (A)

[EN] C stores 2D arrays in row-major order, so incrementing j fastest walks contiguous memory and boosts CPU cache hits.

[HI] C में एरे रो-मेजर क्रम में होते हैं, अतः आंतरिक लूप में कॉलम j चलाने से लगातार मेमोरी एक्सेस होती है और कैश प्रदर्शन बढ़ता है।

Q15. Which statement is NOT true about the for loop in C?

(C में for लूप के बारे में इनमें से कौन सा कथन असत्य है?)

(A) Variables can be declared in init starting in C99

(B) All three expressions (init, cond, update) must always be non-empty

(C) for(;;) creates an infinite loop

(D) Nested for loops yield multiplicative complexity

Correct Answer: (B)

[EN] Every expression inside for( ; ; ) is optional; only the two semicolons are mandatory.

[HI] for( ; ; ) के भीतर की तीनों अभिव्यक्तियाँ वैकल्पिक होती हैं; उनका भरा होना अनिवार्य नहीं है।

Q16. What happens in 'for (int i = 1; i <= 5; ) printf("%d", i);'?

('for (int i = 1; i <= 5; ) printf("%d", i);' चलाने पर क्या होगा?)

(A) Prints 12345

(B) Prints 1 infinitely

(C) Compile error due to empty update

(D) Prints 1 once and stops

Correct Answer: (B)

[EN] The empty update clause is valid syntax, and since i stays 1 inside the body, 1 <= 5 remains forever true.

[HI] अपडेट भाग खाली छोड़ना वैध है, लेकिन बॉडी में भी i न बढ़ने के कारण i हमेशा 1 रहकर अनंत बार '1' प्रिंट करेगा।

Q17. Within Loops in C: for Loop (Initialization; Condition; Increment), which for header part directly maps to: "Runs once before the first condition test"?

(Loops in C: for Loop (Initialization; Condition; Increment) में कौन सा for Header Part सीधे "Runs once before the first condition test" से संबंधित है?)

(A) init (expr1)

(B) update (expr3)

(C) cond (expr2)

(D) for (;;) idiom

Correct Answer: (A)

[EN] In Loops in C: for Loop (Initialization; Condition; Increment), 'init (expr1)' handles 'Runs once before the first condition test' (C99 allows 'int i = 0' scoped only to the loop).

[HI] Loops in C: for Loop (Initialization; Condition; Increment) में 'init (expr1)' का कार्य/विवरण 'Runs once before the first condition test' (C99 allows 'int i = 0' scoped only to the loop) है।

Q18. Which option accurately describes 'cond (expr2)' in Loops in C: for Loop (Initialization; Condition; Increment)?

(Loops in C: for Loop (Initialization; Condition; Increment) के संदर्भ में 'cond (expr2)' की सही विशेषता कौन सी है?)

(A) Runs after body prior to next cond check

(B) Empty clauses separated by two semicolons

(C) Runs once before the first condition test

(D) Tested before every pass; blank equals 1 (true)

Correct Answer: (D)

[EN] 'cond (expr2)' provides 'Tested before every pass; blank equals 1 (true)' — key point: Comma 'a<5, b>0' obeys only rightmost test.

[HI] 'cond (expr2)' का मुख्य विवरण 'Tested before every pass; blank equals 1 (true)' है (Comma 'a<5, b>0' obeys only rightmost test)।

Q19. In Loops in C: for Loop (Initialization; Condition; Increment), identify the correct parameter for the specification: "Runs after body prior to next cond check".

(Loops in C: for Loop (Initialization; Condition; Increment) में "Runs after body prior to next cond check" विवरण किस विकल्प के लिए सही है?)

(A) update (expr3)

(B) init (expr1)

(C) cond (expr2)

(D) for (;;) idiom

Correct Answer: (A)

[EN] 'update (expr3)' corresponds to 'Runs after body prior to next cond check' (Target where 'continue' transfers control).

[HI] 'update (expr3)' का संबंध 'Runs after body prior to next cond check' (Target where 'continue' transfers control) से है।

Q20. Considering Loops in C: for Loop (Initialization; Condition; Increment), what key behavior defines 'for (;;) idiom'?

(Loops in C: for Loop (Initialization; Condition; Increment) में 'for (;;) idiom' की मुख्य भूमिका या मान क्या है?)

(A) Empty clauses separated by two semicolons

(B) Runs after body prior to next cond check

(C) Tested before every pass; blank equals 1 (true)

(D) Runs once before the first condition test

Correct Answer: (A)

[EN] 'for (;;) idiom' represents 'Empty clauses separated by two semicolons' (Canonical infinite loop requiring break/return).

[HI] 'for (;;) idiom' का अर्थ/मान 'Empty clauses separated by two semicolons' है (Canonical infinite loop requiring break/return)।

Loops in C: for Loop (Initialization; Condition; Increment) — Exam FAQs

Q1: Why is for(;;) valid as an infinite loop while while() is a syntax error?

for(;;) अनंत लूप के रूप में वैध क्यों है जबकि while() सिंटैक्स एरर देता है?

[EN] ISO C grammar defines each of the three clauses in for(opt1; opt2; opt3) as optional and defaults an empty middle condition to non-zero (1), whereas while(expr) strictly requires a non-empty controlling expression.

[HI] मानक C व्याकरण में for की तीनों अभिव्यक्तियाँ वैकल्पिक हैं और बीच की शर्त खाली होने पर वह 1 (सत्य) मानी जाती है, जबकि while(expr) में एक मान्य शर्त लिखना अनिवार्य होता है।

Q2: What happens if you put two conditions separated by a comma in a for loop?

यदि for लूप की शर्त वाले हिस्से में कॉमा से अलग दो शर्तें लिखी जाएं तो क्या होगा?

[EN] The C comma operator evaluates the left condition, discards its boolean result completely, and uses only the rightmost condition to decide loop continuation. Use && or || to combine conditions logically.

[HI] C का कॉमा ऑपरेटर बाईं शर्त का मूल्यांकन कर उसका परिणाम फेंक देता है और केवल सबसे दाईं शर्त के आधार पर लूप चलाता है। दोनों शर्तें जांचने के लिए && या || उपयोग करें।

Q3: Can a variable declared inside a C99 for loop header be accessed after the loop?

क्या C99 के for लूप हेडर में घोषित वेरिएबल को लूप के बाहर उपयोग किया जा सकता है?

[EN] No. In C99 and newer standards, 'for (int i = 0; ...)' scopes 'i' strictly to the loop header and body; accessing 'i' afterward fails to compile unless another 'i' exists in an enclosing scope.

[HI] नहीं। C99 और उसके बाद के मानकों में for हेडर में घोषित वेरिएबल का स्कोप केवल उस लूप तक सीमित रहता है; बाहर एक्सेस करने के लिए उसे लूप से पहले घोषित करना होगा।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)