PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Loops in C: do-while Loop (Exit-Controlled Loop) (do-while लूप: एक्जिट-कंट्रोल्ड लूप (कम से कम एक बार निष्पादन)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: The do-while loop is an exit-controlled (post-test) construct that runs its body first before evaluating the trailing while (condition); test. Because testing happens at exit, the body executes at least one time even if the condition is initially false, and a trailing semicolon is mandatory.
अध्याय सार: do-while लूप एक एक्जिट-कंट्रोल्ड (Post-test) लूप है जो पहले अपनी बॉडी को कम से कम एक बार चलाता है और फिर अंत में while (condition); शर्त जांचता है। शर्त अंत में जांची जाने के कारण प्रारंभिक शर्त असत्य होने पर भी बॉडी कम से कम 1 बार अवश्य चलती है, तथा इसके अंत में सेमीकोलन (;) अनिवार्य है।
Loops in C: do-while Loop (Exit-Controlled Loop): Detailed Notes & Architecture (do-while लूप: एक्जिट-कंट्रोल्ड लूप (कम से कम एक बार निष्पादन))
1. Exit-Controlled Flow & Guaranteed Single Execution (एक्जिट-कंट्रोल्ड प्रवाह व न्यूनतम 1 बार निष्पादन)
[EN] In 'do { body } while (condition);', execution enters body immediately without testing condition first. Once body finishes (or hits continue;), condition is evaluated: any non-zero value loops control back to the top of do, while 0 exits cleanly. Consequently, unlike while and for (whose minimum count is 0), do-while guarantees a minimum of 1 execution even for 'do { ... } while (0);'.
[HI] do { body } while (condition); में कंट्रोल बिना कोई शर्त जांचे सीधे लूप बॉडी में प्रवेश करता है। बॉडी पूरी होने (या continue; मिलने) पर अंत में condition जांची जाती है: गैर-शून्य होने पर कंट्रोल वापस do पर जाता है और 0 होने पर लूप समाप्त होता है। इसी कारण while व for (न्यूनतम 0 बार) के विपरीत do-while कम से कम 1 बार अवश्य चलता है।
2. Mandatory Trailing Semicolon & Block-Scope Pitfall (अनिवार्य अंतिम सेमीकोलन व स्कोप सावधानी)
[EN] ISO C strictly mandates a semicolon immediately following while (condition); to terminate the statement; omitting it generates a syntax error. Additionally, variables declared inside the do { ... } compound block go out of scope at its closing brace '}', meaning the trailing while (cond); expression CANNOT reference variables declared inside do—any loop-control variable checked in while must be declared prior to do.
[HI] मानक C में while (condition); के ठीक बाद सेमीकोलन लगाना अनिवार्य है, जिसे छोड़ने पर कंपाइल एरर आता है। एक महत्वपूर्ण सावधानी यह है कि do { ... } के भीतर घोषित वेरिएबल का स्कोप '}' पर ही समाप्त हो जाता है, अतः अंत में लिखी गई while शर्त के अंदर उन स्थानीय वेरिएबल्स को उपयोग नहीं किया जा सकता; कंट्रोल वेरिएबल को do से पहले घोषित करना जरूरी है।
3. Menu-Driven Programs, Input Validation & Digit Counting (मेन्यू-ड्रिवन प्रोग्राम, इनपुट वैलिडेशन व अंक गिनती)
[EN] do-while models tasks that naturally perform an action before checking whether to repeat—such as displaying an interactive menu before reading exit confirmation, re-prompting until valid input arrives, or counting digits in non-negative integers including 0 (where n = 0 correctly records 1 digit rather than 0). Using do-while avoids duplicating prompt logic before a standard while loop.
[HI] do-while उन कार्यों के लिए सर्वोत्तम है जहाँ पहले कार्य होना और बाद में दोहराने का निर्णय लेना आवश्यक हो—जैसे यूजर को कम से कम एक बार मेनू दिखाकर Exit चुनने तक दोहराना, सही इनपुट आने तक पुनः पूछना, या 0 सहित संख्याओं के अंक गिनना। इससे while लूप से पहले कोड दोहराने की आवश्यकता नहीं पड़ती।
4. The do { ... } while (0) Multi-Statement Macro Idiom (सिस्टम C में do { ... } while (0) मैक्रो तकनीक)
[EN] Although do { ... } while (0); executes only once, systems programmers wrap multi-statement #define macros inside 'do { stmt1; stmt2; } while (0)' (leaving out the final semicolon in the macro definition). When a caller writes 'if (ok) MY_MACRO(); else handle_err();', the macro expands cleanly as one single statement terminated by the caller's semicolon, eliminating dangling-else errors and isolating temporary macro variables.
[HI] यद्यपि do { ... } while (0); केवल एक बार चलता है, पर लिनक्स कर्नेल व C हेडर फाइलों में बहु-स्टेटमेंट #define मैक्रो को 'do { s1; s2; } while (0)' में बंद किया जाता है (बिना अंतिम सेमीकोलन के)। इससे कॉलर द्वारा 'if (ok) MY_MACRO(); else ...' लिखने पर सेमीकोलन सही ढंग से जुड़ता है और डैंगलिंग-else की त्रुटि कभी नहीं आती।
Syntax, Formula & Technical Example (Loops in C: do-while Loop (Exit-Controlled Loop))
#include <stdio.h>
int main(void) {
int n = 0, digits = 0;
do {
digits++; // Runs at least once even when n = 0
n /= 10;
} while (n > 0); // Semicolon is strictly mandatory
printf("Digits = %d\n", digits); // Output: Digits = 1
return 0;
}
Loops in C: do-while Loop (Exit-Controlled Loop) Quick Reference & Comparison Table
| do-while Attribute | Behavior in C | Comparison / Exam Key |
|---|---|---|
| Loop Classification | Exit-controlled (post-test) loop | while and for test condition at entry |
| Minimum Body Runs | Guaranteed at least 1 execution | Runs once even for false while(0) |
| Trailing Semicolon | Mandatory right after while (cond); | Omission causes compile-time syntax error |
| do { ... } while(0) | Single-pass #define macro wrapper | Prevents dangling-else bugs inside if-else |
Loops in C: do-while Loop (Exit-Controlled Loop) Top 20 Bilingual MCQs with Answers
Q1. Why is the do-while loop termed an exit-controlled loop?
(do-while लूप को एक्जिट-कंट्रोल्ड लूप क्यों कहा जाता है?)
(A) It cannot contain break statements
(B) Its continuation condition is checked after executing the loop body
(C) It exits immediately without running any code
(D) It checks the condition prior to the first run
Correct Answer: (B)
[EN] Testing occurs at the exit point of each iteration, guaranteeing the body executes before the condition is evaluated.
[HI] प्रत्येक चक्र में बॉडी चलने के बाद बाहर निकलते समय शर्त जांची जाती है, इसलिए इसे एक्जिट-कंट्रोल्ड कहते हैं।
Q2. What is the minimum number of times a do-while loop body executes even if the condition is initially false?
(प्रारंभिक शर्त असत्य होने पर भी do-while लूप की बॉडी न्यूनतम कितनी बार चलती है?)
(A) 0 times
(B) Depends on compiler flags
(C) 1 time
(D) 2 times
Correct Answer: (C)
[EN] Because the condition sits at the bottom after the body, the body runs once before the false test stops the loop.
[HI] शर्त लूप के अंत में होने के कारण लूप रुकने से पहले कम से कम 1 बार बॉडी अवश्य निष्पादित होती है।
Q3. What happens if the semicolon after while (condition) in a do-while loop is omitted?
(do-while लूप में while (condition) के बाद का सेमीकोलन छूट जाने पर क्या होता है?)
(A) It turns into an infinite loop
(B) The compiler adds it silently
(C) It behaves like a normal while loop
(D) Compile-time syntax error
Correct Answer: (D)
[EN] ISO C grammar mandates a semicolon terminating the do-while statement; leaving it out always fails compilation.
[HI] मानक C व्याकरण में do-while के अंत में सेमीकोलन अनिवार्य है, जिसके बिना कंपाइलेशन फेल हो जाता है।
Q4. Why does 'do { int k = 1; k++; } while (k < 5);' fail to compile?
('do { int k = 1; k++; } while (k < 5);' कंपाइल होने में विफल क्यों होता है?)
(A) do-while cannot contain variable declarations
(B) k is declared inside the do block and goes out of scope before while (k < 5)
(C) k must start at 0
(D) int variables cannot be incremented in do-while
Correct Answer: (B)
[EN] Variables inside { ... } die at the closing brace '}', so the trailing while clause cannot access k.
[HI] ब्रेसिज़ के अंदर घोषित k का स्कोप '}' पर समाप्त हो जाता है, जिससे अंत की while शर्त उसे पहचान नहीं पाती।
Q5. What is the output of: int x = 10; do { printf("%d", x); x++; } while (0);?
('int x = 10; do { printf("%d", x); x++; } while (0);' का आउटपुट क्या होगा?)
(A) Infinite 10
(B) Nothing (0 runs)
(C) 10
(D) 10 11
Correct Answer: (C)
[EN] The body runs once printing 10 and setting x = 11, then while (0) evaluates false and exits.
[HI] बॉडी एक बार चलकर 10 प्रिंट करती है और फिर while (0) असत्य होने से लूप तुरंत रुक जाता है।
Q6. When 'continue;' executes inside a do-while loop, where does control jump?
(जब do-while लूप के भीतर 'continue;' चलता है, तो कंट्रोल कहाँ जाता है?)
(A) Directly back to 'do' skipping the condition check
(B) To the while (condition); test at the bottom of the loop
(C) Out of the loop completely
(D) To the next statement after the semicolon
Correct Answer: (B)
[EN] continue skips the rest of the body and jumps to the loop continuation test (while condition) to decide whether to repeat.
[HI] continue शेष बॉडी छोड़कर नीचे की while शर्त की जांच पर जाता है ताकि तय हो सके कि अगला चक्र चलेगा या नहीं।
Q7. Why do professional C headers wrap multi-statement #define macros inside 'do { ... } while (0)'?
(पेशेवर C हेडर फाइलों में बहु-स्टेटमेंट #define मैक्रो को 'do { ... } while (0)' में क्यों लपेटा जाता है?)
(A) To make the macro run faster than functions
(B) To turn multiple statements into one safe unit compatible with unbraced if-else and trailing ';'
(C) To repeat the macro infinitely
(D) To disable compiler type checking
Correct Answer: (B)
[EN] Without do-while(0), calling 'if (c) MACRO(); else ...' breaks the if-else association or leaves stray semicolons.
[HI] इससे पूरा मैक्रो एक एकल स्टेटमेंट की तरह व्यवहार करता है, जिससे बिना ब्रेसिज़ वाले if-else में उपयोग करने पर सिंटैक्स व डैंगलिंग-else त्रुटि नहीं होती।
Q8. Compare outputs for a = 5: 'while(a < 5) printf("%d", a);' vs 'do printf("%d", a); while(a < 5);'.
(a = 5 के लिए 'while(a < 5) printf("%d", a);' तथा 'do printf("%d", a); while(a < 5);' का आउटपुट क्रमशः क्या होगा?)
(A) Both print nothing
(B) while prints nothing; do-while prints 5
(C) Both print 5
(D) while prints 5; do-while prints nothing
Correct Answer: (B)
[EN] a < 5 is false immediately: entry-controlled while skips completely, whereas exit-controlled do-while prints 5 once.
[HI] a < 5 तुरंत असत्य है; अतः एंट्री-कंट्रोल्ड while कुछ प्रिंट नहीं करेगा जबकि एक्जिट-कंट्रोल्ड do-while एक बार 5 प्रिंट करेगा।
Q9. What is the exact output of: int x = 3; do { printf("%d ", x); } while (--x);?
('int x = 3; do { printf("%d ", x); } while (--x);' का सटीक आउटपुट क्या होगा?)
(A) 3 2 1 0
(B) 3 2 1
(C) 2 1 0
(D) 3 2
Correct Answer: (B)
[EN] Pass 1 prints 3 (--x=2), Pass 2 prints 2 (--x=1), Pass 3 prints 1 (--x=0 stops loop).
[HI] पहले चरण में 3 (--x=2), दूसरे में 2 (--x=1) और तीसरे में 1 (--x=0 पर लूप बंद) प्रिंट होता है।
Q10. What is the exact output of: int x = 3; do { printf("%d ", x); } while (x--);?
('int x = 3; do { printf("%d ", x); } while (x--);' का सटीक आउटपुट क्या होगा?)
(A) Infinite loop
(B) 3 2 1 0
(C) 2 1 0
(D) 3 2 1
Correct Answer: (B)
[EN] Post-decrement tests old x: prints 3 (3->2), prints 2 (2->1), prints 1 (1->0), prints 0 (0->-1 stops loop).
[HI] x-- पुराना मान जांचता है: 3, 2, 1 के बाद 0 भी प्रिंट होगा और तब x-- का पुराना मान 0 मिलने पर लूप रुकेगा; उत्तर 3 2 1 0।
Q11. Are curly braces { } syntactically required in do-while when the loop body has only one statement?
(जब do-while की बॉडी में केवल एक ही स्टेटमेंट हो, तो क्या ब्रेसिज़ { } लगाना व्याकरण की दृष्टि से अनिवार्य है?)
(A) Yes, unless break is used
(B) No, 'do stmt; while (cond);' is valid syntax for a single statement
(C) Only if cond uses >
(D) Yes, braces are mandatory in do-while even for 1 statement
Correct Answer: (B)
[EN] Single-statement bodies work without braces in C, though braces are strongly recommended for readability.
[HI] एकल स्टेटमेंट के लिए ब्रेसिज़ अनिवार्य नहीं हैं ('do printf("A"); while(0);' वैध है), पर स्पष्टता हेतु लगाना अच्छा माना जाता है।
Q12. What happens in: do { printf("A"); break; } while (1);?
('do { printf("A"); break; } while (1);' का क्या परिणाम होगा?)
(A) Prints A infinitely
(B) Compile error
(C) Skips printf and runs while(1) forever
(D) Prints A once and exits the loop immediately
Correct Answer: (D)
[EN] break terminates the enclosing do-while immediately before reaching the while(1) condition.
[HI] break तुरंत पूरे do-while लूप को समाप्त कर देता है, अतः एक बार A प्रिंट होकर लूप बाहर निकल आता है।
Q13. Which condition creates an infinite do-while loop (assuming no break or return inside)?
(बिना break या return के इनमें से कौन सी शर्त अनंत do-while लूप बनाएगी?)
(A) do { ... } while (0);
(B) do { ... } while (-1);
(C) do { ... } while (1 - 1);
(D) do { ... } while ('\0');
Correct Answer: (B)
[EN] Any non-zero constant (-1) evaluates to true; 0, '\0' (null char = 0), and 1-1 (= 0) all stop after 1 pass.
[HI] -1 एक गैर-शून्य (सत्य) मान है जिससे अनंत लूप बनेगा, जबकि 0, '\0' (मान 0) और 1-1 (मान 0) पहली बार के बाद रुक जाएंगे।
Q14. Which programming scenario is most naturally suited to a do-while loop?
(इनमें से कौन सा प्रोग्रामिंग परिदृश्य do-while लूप के लिए सबसे स्वाभाविक है?)
(A) Displaying a CLI menu at least once and repeating until the user selects Exit
(B) Scanning an empty array of length 0
(C) Iterating a known count from 0 to 99
(D) Checking whether a pointer is NULL before dereferencing
Correct Answer: (A)
[EN] Menus must display at least once before evaluating the user's exit selection; arrays of size 0 require pre-test while/for.
[HI] मेनू कम से कम एक बार दिखाकर यूजर की पसंद के अनुसार दोहराना do-while का आदर्श उपयोग है; शून्य लंबाई एरे में pre-test लूप चाहिए।
Q15. Which statement is NOT true about the do-while loop in C?
(C में do-while लूप के बारे में इनमें से कौन सा कथन असत्य है?)
(A) If the initial condition is false, the do-while body runs zero times
(B) A semicolon is mandatory after while (condition)
(C) It is an exit-controlled construct
(D) Its body executes at least once unconditionally
Correct Answer: (A)
[EN] Even if the initial condition is false, do-while always runs the body at least once before testing.
[HI] प्रारंभिक शर्त असत्य होने पर भी do-while कम से कम 1 बार अवश्य चलता है, 0 बार नहीं।
Q16. Why is do { digits++; n /= 10; } while (n > 0); safer than while (n > 0) for counting digits of integer n >= 0?
(n >= 0 के अंक गिनने के लिए while (n > 0) की तुलना में do { digits++; n /= 10; } while (n > 0); क्यों बेहतर है?)
(A) It correctly counts n = 0 as having 1 digit instead of 0 digits
(B) It works for negative numbers without abs()
(C) It avoids integer division completely
(D) It executes in O(1) time for 100-digit numbers
Correct Answer: (A)
[EN] For n = 0, while (n > 0) exits immediately with digits = 0, whereas do-while counts 1 digit.
[HI] n = 0 देने पर while (n > 0) बिना चले 0 अंक बताता है, जबकि do-while कम से कम एक बार चलकर सही उत्तर 1 अंक देता है।
Q17. Within Loops in C: do-while Loop (Exit-Controlled Loop), which do-while attribute directly maps to: "Exit-controlled (post-test) loop"?
(Loops in C: do-while Loop (Exit-Controlled Loop) में कौन सा do-while Attribute सीधे "Exit-controlled (post-test) loop" से संबंधित है?)
(A) do { ... } while(0)
(B) Trailing Semicolon
(C) Minimum Body Runs
(D) Loop Classification
Correct Answer: (D)
[EN] In Loops in C: do-while Loop (Exit-Controlled Loop), 'Loop Classification' handles 'Exit-controlled (post-test) loop' (while and for test condition at entry).
[HI] Loops in C: do-while Loop (Exit-Controlled Loop) में 'Loop Classification' का कार्य/विवरण 'Exit-controlled (post-test) loop' (while and for test condition at entry) है।
Q18. Which option accurately describes 'Minimum Body Runs' in Loops in C: do-while Loop (Exit-Controlled Loop)?
(Loops in C: do-while Loop (Exit-Controlled Loop) के संदर्भ में 'Minimum Body Runs' की सही विशेषता कौन सी है?)
(A) Mandatory right after while (cond);
(B) Exit-controlled (post-test) loop
(C) Single-pass #define macro wrapper
(D) Guaranteed at least 1 execution
Correct Answer: (D)
[EN] 'Minimum Body Runs' provides 'Guaranteed at least 1 execution' — key point: Runs once even for false while(0).
[HI] 'Minimum Body Runs' का मुख्य विवरण 'Guaranteed at least 1 execution' है (Runs once even for false while(0))।
Q19. In Loops in C: do-while Loop (Exit-Controlled Loop), identify the correct parameter for the specification: "Mandatory right after while (cond);".
(Loops in C: do-while Loop (Exit-Controlled Loop) में "Mandatory right after while (cond);" विवरण किस विकल्प के लिए सही है?)
(A) Trailing Semicolon
(B) Minimum Body Runs
(C) do { ... } while(0)
(D) Loop Classification
Correct Answer: (A)
[EN] 'Trailing Semicolon' corresponds to 'Mandatory right after while (cond);' (Omission causes compile-time syntax error).
[HI] 'Trailing Semicolon' का संबंध 'Mandatory right after while (cond);' (Omission causes compile-time syntax error) से है।
Q20. Considering Loops in C: do-while Loop (Exit-Controlled Loop), what key behavior defines 'do { ... } while(0)'?
(Loops in C: do-while Loop (Exit-Controlled Loop) में 'do { ... } while(0)' की मुख्य भूमिका या मान क्या है?)
(A) Single-pass #define macro wrapper
(B) Mandatory right after while (cond);
(C) Guaranteed at least 1 execution
(D) Exit-controlled (post-test) loop
Correct Answer: (A)
[EN] 'do { ... } while(0)' represents 'Single-pass #define macro wrapper' (Prevents dangling-else bugs inside if-else).
[HI] 'do { ... } while(0)' का अर्थ/मान 'Single-pass #define macro wrapper' है (Prevents dangling-else bugs inside if-else)।
Loops in C: do-while Loop (Exit-Controlled Loop) — Exam FAQs
Q1: Why does do-while require a trailing semicolon whereas while and for do not?
while और for के विपरीत do-while के अंत में सेमीकोलन क्यों आवश्यक है?
[EN] while and for end with their loop body statement or block '}', whereas do-while places its while (condition) test at the end; C uses ';' to clearly mark the end of the do-while construct and distinguish it from a new while loop.
[HI] while और for लूप '}' या बॉडी स्टेटमेंट पर खत्म होते हैं, जबकि do-while के अंत में while (condition) आता है; यह कोई नया while लूप न लगे, इसलिए वाक्य समाप्ति के लिए ';' जरूरी होता है।
Q2: Why do C programmers write do { ... } while (0) around macros?
C प्रोग्रामर मैक्रो के चारों ओर do { ... } while (0) क्यों लिखते हैं?
[EN] It wraps multiple statements into a single compound unit that safely absorbs a caller's semicolon inside unbraced if-else branches without breaking else pairing.
[HI] यह कई स्टेटमेंट्स को एक सुरक्षित इकाई में बांधता है ताकि बिना ब्रेसिज़ वाले if-else में उपयोग करने पर कॉलर का सेमीकोलन सिंटैक्स या डैंगलिंग-else त्रुटि न पैदा करे।
Q3: How does continue behave inside a do-while loop?
do-while लूप के भीतर continue कैसे व्यवहार करता है?
[EN] continue skips all remaining statements in the current iteration and jumps straight to the trailing while (condition); test to decide whether to repeat.
[HI] continue वर्तमान चक्र के शेष स्टेटमेंट्स को छोड़कर सीधे नीचे दी गई while (condition); शर्त की जांच पर ले जाता है।
Post a Comment
Post a Comment