Loops in C: while Loop (Entry-Controlled Loop) Notes in Hindi & English : while लूप: एंट्री-कंट्रोल्ड लूप की कार्यप्रणाली और शर्तें और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

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

Overview: The while loop is an entry-controlled (pre-test) construct that evaluates its scalar condition prior to every iteration, including the very first pass. If the initial condition evaluates to zero, the body executes zero times, making while ideal for sentinel-controlled and unknown-length loops.

अध्याय सार: while लूप एक एंट्री-कंट्रोल्ड (Pre-test) लूप है जो बॉडी के प्रत्येक निष्पादन—यहाँ तक कि पहले चरण—से पहले शर्त का मूल्यांकन करता है। यदि प्रारंभिक शर्त ही 0 (असत्य) हो, तो लूप बॉडी 0 बार चलती है, जिससे यह अनिश्चित पुनरावृत्ति संख्या और सेंटिनल-आधारित कार्यों के लिए आदर्श है।

Loops in C: while Loop (Entry-Controlled Loop): Detailed Notes & Architecture (while लूप: एंट्री-कंट्रोल्ड लूप की कार्यप्रणाली और शर्तें)

1. Entry-Controlled Execution Cycle & Zero Minimum Runs (एंट्री-कंट्रोल्ड चक्र व शून्य न्यूनतम निष्पादन)

[EN] In 'while (condition) { body }', C evaluates condition first before entering body. Any non-zero scalar value (positive, negative, or non-NULL pointer) is treated as true, running the body once and jumping back to re-test condition; evaluation continues cyclically until condition yields 0. Because the guard sits at loop entry, a initially false condition skips the body completely, giving a minimum execution count of 0.

[HI] while (condition) { body } में प्रोग्राम पहले condition की जांच करता है। शर्त का मान कोई भी गैर-शून्य संख्या होने पर बॉडी एक बार चलती है और कंट्रोल पुनः शर्त जांचने ऊपर लौटता है; 0 मिलने पर लूप रुक जाता है। शर्त प्रवेश द्वार पर ही जांचे जाने के कारण यदि प्रारंभ में ही शर्त असत्य हो तो बॉडी 0 बार चलती है।

2. Loop Components Placement & Infinite while Loops (लूप घटकों की स्थिति व अनंत while लूप)

[EN] Unlike for, the while header accepts only a test expression; loop variable initialization must precede the while statement and the update step must reside inside the body (or inside the condition). Omitting the update leaves the test permanently true, creating an infinite loop—deliberately expressed as while (1) or while (true) via <stdbool.h> for event servers and embedded firmware. Unlike for (;;), leaving parentheses empty as while () is a compile-time syntax error.

[HI] for लूप के विपरीत while हेडर में केवल शर्त लिखी जाती है; वेरिएबल इनिशियलाइजेशन लूप से पहले और अपडेट बॉडी के अंदर किया जाता है। अपडेट न लिखने पर अनंत लूप बन जाता है, जिसे एम्बेडेड सिस्टम या सर्वर में जानबूझकर while (1) के रूप में उपयोग करते हैं। ध्यान रहे कि for (;;) के विपरीत while () को खाली छोड़ना सिंटैक्स एरर होता है।

3. Semicolon Hang Trap, Unsigned Wrap & Condition Side Effects (सेमीकोलन त्रुटि, अनसाइंड रैप व साइड इफेक्ट्स)

[EN] Appending a stray semicolon directly after the header (while (cond);) binds a null statement as the entire body, often hanging the program forever because the update inside the following { ... } block is detached and never reached. Another subtle bug is 'unsigned int u = 3; while (u >= 0) u--;' which loops forever because unsigned integers wrap from 0 to UINT_MAX and can never be negative. Idiomatic C frequently embeds assignment inside conditions, such as 'while ((ch = getchar()) != EOF)' using int ch.

[HI] हेडर के ठीक बाद गलती से सेमीकोलन लगा देने (while (cond);) से खाली स्टेटमेंट लूप बॉडी बन जाता है और बाद वाला ब्लॉक अलग हो जाने से प्रोग्राम अनंत लूप में अटक सकता है। इसी प्रकार unsigned int u के साथ while (u >= 0) लिखना भी अनंत लूप बनाता है क्योंकि अनसाइंड मान कभी ऋणात्मक नहीं होता। दूसरी ओर C में while ((ch = getchar()) != EOF) जैसे एक्सप्रेशन बहुत प्रचलित हैं जहाँ इनपुट और जांच एक साथ होती है।

4. Classic Applications: Digit Extraction, Euclid GCD & Float Risks (प्रमुख अनुप्रयोग: अंक विभाजन, GCD व फ्लोट जोखिम)

[EN] The while loop shines whenever iteration count depends on runtime data rather than fixed counters—such as digit reversal (n /= 10), linked list traversal (ptr != NULL), Euclid's GCD (b != 0), or stream parsing. Avoid exact floating-point equality conditions like 'while (x != 1.0) x += 0.1;' because binary rounding imprecision can step past 1.0 and trigger an unintended infinite loop; always prefer inequality tests like x < 1.0.

[HI] जब चक्रों की संख्या रनटाइम डेटा पर निर्भर हो—जैसे संख्या के अंक गिनना/उलटना (n /= 10), लिंक्ड लिस्ट ट्रैवर्सल (ptr != NULL), यूक्लिड का GCD या फाइल रीडिंग—तब while लूप सर्वोत्तम है। फ्लोटिंग-पॉइंट मानों में while (x != 1.0) जैसी सटीक समानता की शर्त से बचना चाहिए क्योंकि बाइनरी राउंडिंग के कारण मान 1.0 को पार कर अनंत लूप बना सकता है; इसके बजाय x < 1.0 लिखना सुरक्षित है।

Syntax, Formula & Technical Example (Loops in C: while Loop (Entry-Controlled Loop))

#include <stdio.h>
int main(void) {
    int n = 452, sum = 0;
    while (n > 0) {          // Entry test before every pass
        sum += n % 10;       // Extracts digits: 2, 5, 4
        n /= 10;             // Update step moves toward termination
    }
    printf("Digit sum = %d\n", sum); // Output: Digit sum = 11
    return 0;
}

Loops in C: while Loop (Entry-Controlled Loop) Quick Reference & Comparison Table

while Loop CharacteristicTechnical Behavior in CCritical Exam Detail
Evaluation TimingTests condition before entering loop bodyEntry-controlled (pre-test) iteration construct
Minimum Executions0 times when initial test is falseContrasts with do-while (>= 1 guaranteed run)
Condition SyntaxExactly one non-empty scalar expressionwhile() fails compilation; while(1) runs forever
Trailing Semicolon ';'Attaches empty null statement as bodyCauses infinite loop if condition variable stops updating

Loops in C: while Loop (Entry-Controlled Loop) Top 20 Bilingual MCQs with Answers

Q1. Why is the while loop classified as an entry-controlled loop?

(while लूप को एंट्री-कंट्रोल्ड लूप क्यों कहा जाता है?)

(A) Its condition is tested before entering the loop body on every iteration

(B) It always enters the loop body at least once

(C) Its condition is tested only after the loop body executes

(D) It requires an integer counter initialized at entry

Correct Answer: (A)

[EN] Entry-controlled means the guard condition must succeed prior to each pass, including the very first pass.

[HI] एंट्री-कंट्रोल्ड का अर्थ है कि पहले चरण सहित प्रत्येक चक्र में लूप बॉडी के अंदर जाने से पहले शर्त की जांच होती है।

Q2. What is the minimum number of times the body of a while loop executes if its initial condition is false?

(यदि प्रारंभिक शर्त असत्य हो, तो while लूप की बॉडी न्यूनतम कितनी बार चलती है?)

(A) 2 times

(B) 0 times

(C) 1 time

(D) Depends on compiler optimization

Correct Answer: (B)

[EN] Because the condition fails on entry before the first pass, the body is bypassed completely (0 runs).

[HI] प्रवेश पर ही शर्त असत्य होने के कारण बॉडी एक बार भी नहीं चलती, यानी न्यूनतम निष्पादन संख्या 0 है।

Q3. Which infinite loop syntax triggers a compile-time error in standard C?

(मानक C में इनमें से कौन सा अनंत लूप सिंटैक्स कंपाइल-टाइम एरर उत्पन्न करेगा?)

(A) while (1) { }

(B) while () { }

(C) while (-5) { }

(D) for (;;) { }

Correct Answer: (B)

[EN] Unlike for(;;), the while parentheses cannot be left empty; they require a scalar condition expression.

[HI] for(;;) के विपरीत while() में कोष्ठक को खाली नहीं छोड़ा जा सकता; उसमें एक मान्य शर्त होना अनिवार्य है।

Q4. What happens when executing: int i = 1; while (i <= 3); { printf("%d", i); i++; }?

(कोड 'int i = 1; while (i <= 3); { printf("%d", i); i++; }' चलाने पर क्या परिणाम मिलेगा?)

(A) Infinite loop with no output printed

(B) Prints 1

(C) Prints 123

(D) Prints 4

Correct Answer: (A)

[EN] The stray semicolon ends an empty while body; i remains 1 forever so control never reaches the printf block.

[HI] while के बाद लगे सेमीकोलन से बॉडी खाली हो जाती है और i हमेशा 1 रहने के कारण लूप अनंत होकर कुछ भी प्रिंट नहीं करता।

Q5. What is the exact output of: int i = 0; while (i++ < 3) printf("%d ", i);?

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

(A) 0 1 2 3

(B) 1 2 3 4

(C) 0 1 2

(D) 1 2 3

Correct Answer: (D)

[EN] Post-increment tests old i against 3 then increments before printf: 0<3 prints 1, 1<3 prints 2, 2<3 prints 3, then 3<3 stops.

[HI] पोस्ट-इंक्रीमेंट में पहले पुराना मान 3 से तुलना होता है फिर i बढ़कर प्रिंट होता है: 0<3 पर 1, 1<3 पर 2, 2<3 पर 3 और 3<3 पर लूप रुकता है।

Q6. What is the exact output of: int i = 0; while (++i < 3) printf("%d ", i);?

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

(A) 2 3

(B) 1 2

(C) 0 1 2

(D) 1 2 3

Correct Answer: (B)

[EN] Pre-increment updates i before comparing: 1<3 prints 1, 2<3 prints 2, then 3<3 is false and loop exits.

[HI] प्री-इंक्रीमेंट में तुलना से पहले i बढ़ता है: 1<3 पर 1, 2<3 पर 2 प्रिंट होता है और 3<3 असत्य होते ही लूप समाप्त हो जाता है।

Q7. What bug occurs in: unsigned int u = 5; while (u >= 0) { printf("%u ", u); u--; }?

(कोड 'unsigned int u = 5; while (u >= 0) { printf("%u ", u); u--; }' में कौन सा बग होगा?)

(A) Prints 5 4 3 2 1 0 and stops

(B) Prints 5 4 3 2 1 and stops

(C) Compile error due to unsigned comparison

(D) Infinite loop because unsigned int can never become negative

Correct Answer: (D)

[EN] When u is 0, u-- wraps around modulo 2^N to UINT_MAX, so u >= 0 remains permanently true.

[HI] u के 0 होने के बाद u-- करने पर अनसाइंड इंटीजर रैप होकर अधिकतम धनात्मक मान बन जाता है, जिससे u >= 0 हमेशा सत्य रहकर अनंत लूप बनाता है।

Q8. How many iterations execute in: int c = -3; while (c) { printf("*"); c++; }?

('int c = -3; while (c) { printf("*"); c++; }' में लूप कितनी बार चलेगा?)

(A) 4 times

(B) 3 times (prints ***)

(C) 0 times because -3 is negative

(D) Infinite loop

Correct Answer: (B)

[EN] In C, negative values are non-zero (true); c takes values -3, -2, -1 (3 passes) and stops when c reaches 0.

[HI] C में ऋणात्मक मान भी गैर-शून्य होने से सत्य होता है; c = -3, -2, -1 पर 3 बार '*' प्रिंट होकर 0 पर रुक जाएगा।

Q9. What prints in: int x = 4; while (x = 0) printf("Run"); printf("%d", x);?

('int x = 4; while (x = 0) printf("Run"); printf("%d", x);' का आउटपुट क्या होगा?)

(A) 0

(B) 4

(C) Run4

(D) Infinite Run

Correct Answer: (A)

[EN] The assignment x = 0 sets x to 0 and evaluates to 0 (false), skipping the loop and printing 0.

[HI] x = 0 असाइनमेंट x को 0 बना देता है और मान 0 (असत्य) होने से लूप बिना चले सीधे 0 प्रिंट होता है।

Q10. How many asterisks print in: int n = 4; while (n-- > 0) printf("*");?

('int n = 4; while (n-- > 0) printf("*");' द्वारा कितने '*' प्रिंट होंगे?)

(A) 4

(B) 5

(C) 3

(D) 0

Correct Answer: (A)

[EN] Post-decrement tests n = 4, 3, 2, 1 (4 true passes) and terminates when old n is 0.

[HI] n-- > 0 पहले n = 4, 3, 2, 1 के लिए सत्य होकर 4 बार चलता है और n = 0 की जांच पर रुक जाता है।

Q11. What does the comma operator cause in: int i = 0, j = 5; while (i < 10, j > 3) { printf("X"); j--; }?

('int i = 0, j = 5; while (i < 10, j > 3) { printf("X"); j--; }' में कॉमा ऑपरेटर के कारण क्या आउटपुट आएगा?)

(A) XXXXXXXXXX (10 times)

(B) Compile error

(C) Infinite X

(D) XX

Correct Answer: (D)

[EN] Comma operator discards i < 10 and returns only the rightmost operand j > 3, running for j = 5 and j = 4.

[HI] कॉमा ऑपरेटर बाईं शर्त i < 10 को अनदेखा कर केवल दाईं शर्त j > 3 का परिणाम लौटाता है, जिससे j=5 और j=4 पर 'XX' प्रिंट होता है।

Q12. In the standard C idiom 'while ((ch = getchar()) != EOF)', why must 'ch' be declared as 'int' instead of 'char'?

(मानक C प्रयोग 'while ((ch = getchar()) != EOF)' में 'ch' को 'char' के बजाय 'int' क्यों घोषित किया जाता है?)

(A) To distinguish EOF (-1) reliably from valid 8-bit unsigned char value 255

(B) Because getchar() returns a 64-bit float

(C) Because char cannot hold ASCII letters

(D) To speed up floating-point division

Correct Answer: (A)

[EN] EOF is negative (-1); on platforms where plain char is unsigned, EOF converts to 255 and collides with valid byte 0xFF.

[HI] EOF का मान -1 होता है; अनसाइंड char वाले सिस्टम पर -1 और वैध बाइट 255 में टकराव से बचने के लिए int लेना आवश्यक है।

Q13. Why is 'double x = 0.0; while (x != 1.0) x += 0.1;' considered hazardous in C?

(C में 'double x = 0.0; while (x != 1.0) x += 0.1;' लिखना जोखिम भरा क्यों माना जाता है?)

(A) Binary floating-point rounding may keep x slightly off 1.0, causing an infinite loop

(B) double variables cannot be used inside while

(C) x += 0.1 truncates to 0 in C

(D) while loops only work with integers

Correct Answer: (A)

[EN] 0.1 has no exact binary representation, so repeated addition often yields 0.9999999999999999 and skips 1.0.

[HI] 0.1 का सटीक बाइनरी रूप न होने से जोड़ने पर मान ठीक 1.0 नहीं बनता और '!=' शर्त हमेशा सत्य रहकर अनंत लूप बना सकती है।

Q14. What is the final value of count for n = 0 in: int n = 0, count = 0; while (n > 0) { count++; n /= 10; }?

('int n = 0, count = 0; while (n > 0) { count++; n /= 10; }' चलने के बाद count का मान क्या होगा?)

(A) 1

(B) Infinite

(C) 0

(D) -1

Correct Answer: (C)

[EN] Since 0 > 0 is immediately false at entry, the body runs zero times and count stays 0.

[HI] प्रारंभ में ही 0 > 0 असत्य है, अतः एंट्री-कंट्रोल्ड while लूप एक बार भी नहीं चलता और count 0 रहता है।

Q15. Can a variable be declared directly inside a C while condition like 'while (int k = getVal())'?

(क्या मानक C में while की शर्त के अंदर 'while (int k = getVal())' जैसा वेरिएबल डिक्लेरेशन लिखा जा सकता है?)

(A) Only for static variables

(B) No, C syntax rejects declarations inside while conditions (unlike C99 for init)

(C) Yes, starting from C99

(D) Yes, starting from ANSI C89

Correct Answer: (B)

[EN] Unlike C++ or C99's for-init clause, C grammar only permits expressions inside while ( ... ), never declarations.

[HI] C++ या C99 के for हेडर के विपरीत मानक C में while(...) के भीतर वेरिएबल डिक्लेरेशन की अनुमति नहीं है।

Q16. Which statement is NOT true about the while loop in C?

(C में while लूप के संबंध में इनमें से कौन सा कथन सत्य नहीं है?)

(A) Its condition is evaluated only once when the loop begins

(B) Its body executes 0 times if the condition is initially false

(C) It requires break, return, or exit to terminate while(1)

(D) Multiple statements inside its body must be enclosed in braces { }

Correct Answer: (A)

[EN] The while condition is re-evaluated before every single iteration, not just once at the start.

[HI] while लूप की शर्त केवल शुरुआत में एक बार नहीं, बल्कि प्रत्येक पुनरावृत्ति से पहले दोबारा जांची जाती है।

Q17. Within Loops in C: while Loop (Entry-Controlled Loop), which while loop characteristic directly maps to: "Tests condition before entering loop body"?

(Loops in C: while Loop (Entry-Controlled Loop) में कौन सा while Loop Characteristic सीधे "Tests condition before entering loop body" से संबंधित है?)

(A) Minimum Executions

(B) Evaluation Timing

(C) Trailing Semicolon ';'

(D) Condition Syntax

Correct Answer: (B)

[EN] In Loops in C: while Loop (Entry-Controlled Loop), 'Evaluation Timing' handles 'Tests condition before entering loop body' (Entry-controlled (pre-test) iteration construct).

[HI] Loops in C: while Loop (Entry-Controlled Loop) में 'Evaluation Timing' का कार्य/विवरण 'Tests condition before entering loop body' (Entry-controlled (pre-test) iteration construct) है।

Q18. Which option accurately describes 'Minimum Executions' in Loops in C: while Loop (Entry-Controlled Loop)?

(Loops in C: while Loop (Entry-Controlled Loop) के संदर्भ में 'Minimum Executions' की सही विशेषता कौन सी है?)

(A) Exactly one non-empty scalar expression

(B) Tests condition before entering loop body

(C) Attaches empty null statement as body

(D) 0 times when initial test is false

Correct Answer: (D)

[EN] 'Minimum Executions' provides '0 times when initial test is false' — key point: Contrasts with do-while (>= 1 guaranteed run).

[HI] 'Minimum Executions' का मुख्य विवरण '0 times when initial test is false' है (Contrasts with do-while (>= 1 guaranteed run))।

Q19. In Loops in C: while Loop (Entry-Controlled Loop), identify the correct parameter for the specification: "Exactly one non-empty scalar expression".

(Loops in C: while Loop (Entry-Controlled Loop) में "Exactly one non-empty scalar expression" विवरण किस विकल्प के लिए सही है?)

(A) Evaluation Timing

(B) Minimum Executions

(C) Condition Syntax

(D) Trailing Semicolon ';'

Correct Answer: (C)

[EN] 'Condition Syntax' corresponds to 'Exactly one non-empty scalar expression' (while() fails compilation; while(1) runs forever).

[HI] 'Condition Syntax' का संबंध 'Exactly one non-empty scalar expression' (while() fails compilation; while(1) runs forever) से है।

Q20. Considering Loops in C: while Loop (Entry-Controlled Loop), what key behavior defines 'Trailing Semicolon ';''?

(Loops in C: while Loop (Entry-Controlled Loop) में 'Trailing Semicolon ';'' की मुख्य भूमिका या मान क्या है?)

(A) 0 times when initial test is false

(B) Tests condition before entering loop body

(C) Exactly one non-empty scalar expression

(D) Attaches empty null statement as body

Correct Answer: (D)

[EN] 'Trailing Semicolon ';'' represents 'Attaches empty null statement as body' (Causes infinite loop if condition variable stops updating).

[HI] 'Trailing Semicolon ';'' का अर्थ/मान 'Attaches empty null statement as body' है (Causes infinite loop if condition variable stops updating)।

Loops in C: while Loop (Entry-Controlled Loop) — Exam FAQs

Q1: When should I choose a while loop instead of a for loop in C?

C में for लूप के बजाय while लूप कब चुनना चाहिए?

[EN] Choose while when the total iteration count is unknown in advance and depends on dynamic conditions—such as reading until EOF, waiting for sentinel input, or traversing linked lists until NULL.

[HI] जब पुनरावृत्ति की संख्या पहले से तय न हो और किसी रनटाइम स्थिति पर निर्भर हो—जैसे EOF तक फाइल पढ़ना, सेंटिनल इनपुट या NULL तक लिंक्ड लिस्ट ट्रैवर्सल—तब while लूप बेहतर रहता है।

Q2: Why does 'while (i >= 0) i--;' create an infinite loop when i is unsigned int?

unsigned int के साथ 'while (i >= 0) i--;' अनंत लूप क्यों बन जाता है?

[EN] An unsigned integer can never hold a negative value; decrementing 0 wraps underflow around to UINT_MAX, keeping i >= 0 permanently true.

[HI] अनसाइंड इंटीजर कभी ऋणात्मक नहीं हो सकता; 0 से घटाने पर मान रैप-अराउंड होकर अधिकतम धनात्मक सीमा (UINT_MAX) पर पहुंच जाता है और शर्त हमेशा सत्य रहती है।

Q3: What happens if you accidentally put a semicolon right after while (cond)?

while (cond) के ठीक बाद गलती से सेमीकोलन लगा देने पर क्या होता है?

[EN] The semicolon acts as an empty null body, detaching the real code block beneath it; if cond variables are only updated inside that detached block, the loop hangs forever.

[HI] सेमीकोलन एक खाली लूप बॉडी बन जाता है जिससे नीचे का वास्तविक ब्लॉक अलग हो जाता है; यदि अपडेट स्टेटमेंट उसी ब्लॉक में हो, तो प्रोग्राम अनंत लूप में फंस जाता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)