Iterative statements: while, do-while, for loop

Loops काम दोहराते हैं: while चलाने से पहले जाँचता है (शायद शून्य बार), do-while बाद में जाँचता है (हमेशा एक बार चलता है), और for counter, condition और update को एक ही line में समेटता है, गिनी-चुनी पुनरावृत्ति के लिए।

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

60 students, program एक बार चला

Marks program एक student को ख़ूबसूरती से सँभालता है। Class में 60 हैं।

आज आपके विकल्प: program 60 बार चलाइए (चपरासी वाला तरीक़ा), code को 60 बार copy-paste कीजिए (तबाही वाला तरीक़ा), या C से कहिए: "इस block को तब तक दोहराओ जब तक मैं रुकने को न कहूँ"।

वह तीसरा विकल्प है iteration, आपके 3 structured-programming आकारों में से आख़िरी, और यह 3 flavours में आता है जिनके फ़र्क़ शुद्ध exam का सोना हैं।

Theory

असल ज़िंदगी में दोहराव के 3 ढंग

while airport security है: guard आपके अंदर घुसने से PEHLE जाँचता है, और पहली जाँच में fail हुए तो आप अंदर घुसते ही नहीं (शून्य entries मुमकिन)। do-while पकाते वक़्त दाल चखना है: आप PEHLE चखते हैं, फिर तय करते हैं कि दोबारा adjust करना है या नहीं, पहला चखना हमेशा होता है। for attendance लेती teacher है: roll 1 से 60, count उसे शुरू करने से पहले पता है और वह उसमें से यंत्रवत गुज़रती है।

Theory

तीनों loops, ठीक-ठीक

  • while (condition) { }: पहले जाँचो, सच हो तो चलाओ, दोहराओ। तुरंत fail? Body शून्य बार चलती है (entry-controlled)।
  • do { } while (condition);: पहले चलाओ, बाद में जाँचो, इसलिए body हमेशा कम से कम एक बार चलती है (exit-controlled)। बंद करने वाला semicolon ध्यान से।
  • for (init; condition; update) { }: गिनती वाला loop। init एक बार चलता है, condition हर दौर से पहले जाँची जाती है, update हर दौर के बाद:

for (i = 1; i <= 60; i++) पढ़िए: student 1 से शुरू करो, 60 के अंदर रहते हुए जारी रखो, एक-एक क़दम बढ़ाओ।

At a glance

अपना loop चुनना

Loopजाँचता हैकम से कम runsकिसके लिए बना
whileBody से पहले0अनजान count, पूरा skip भी हो सकता है
do-whileBody के बाद1Menus: कम से कम एक बार दिखे
forपहले, counter के साथ0पता count (60 students)

Practical

पूरी class, एक loop

#include <stdio.h>

int main() {
    int i, marks, total = 0;

    for (i = 1; i <= 5; i++) {
        printf("Marks of student %d: ", i);
        scanf("%d", &marks);
        total = total + marks;
    }
    printf("Class total: %d\n", total);
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

एक while loop की condition पहली ही बार झूठ है। वही condition एक do-while में है। हर body कितनी बार चलती है?

  1. while: 0 बार, do-while: 1 बार
  2. दोनों 0 बार चलती हैं
  3. दोनों 1 बार चलती हैं
  4. while: 1 बार, do-while: 0 बार
Show the answer

while: 0 बार, do-while: 1 बार

while entry-controlled है: gate पर झूठी जाँच मतलब body कभी नहीं चलती। do-while exit-controlled है: पहली जाँच से पहले body PEHLE ही एक बार चल चुकी होती है। यह 0-बनाम-1 वाला फ़र्क़ हर C paper का सबसे भरोसेमंद loop सवाल है, अक्सर "while और do-while में अंतर" के रूप में।

Think first

Loop trace कीजिए

ऊपर के code में डाले गए 5 marks हैं 70, 80, 60, 90, 75। Loop मन में चलाइए: हर दौर के बाद total क्या है, और अंत में क्या print होता है?

Show the answer

total बढ़ता है 0 → 70 → 150 → 210 → 300 → 375, और program print करता है Class total: 375।

जो pattern आपने अभी इस्तेमाल किया उस पर ग़ौर कीजिए: 0 से शुरू होता एक running total, loop के अंदर update होता हुआ। वह accumulator pattern आपकी बाक़ी programming ज़िंदगी में averages, counts और sums में फिर-फिर लौटता है।

Watch out

3 loop killers

आवारा semicolon: for (i = 1; i <= 5; i++); loop को ख़ाली body के साथ ख़त्म कर देता है; नीचे का block एक बार चलता है। वही बीमारी जो if के semicolon की है। Infinite loop: update (i++) भूल जाइए और condition कभी झूठ नहीं होती; program अटक जाता है। Off-by-one: i < 5 0 से शुरू होकर 5 बार चलता है, i <= 5 0 से शुरू होकर 6 बार, exam में हर बार उँगलियों पर गिनिए।

Theory

तीनों आकार हासिल

Sequence, selection, और अब iteration: marks program हर structured-programming आकार इस्तेमाल करता है, बिल्कुल जैसा Unit 1 में वादा था। 2 सुधार loop की कहानी पूरी करते हैं: loops के अंदर loops (pattern-print वाले सवाल जो exams को बेहद पसंद हैं), और आपातकालीन निकास break तथा continue। दोनों आगे हैं।

Summary

Key takeaways

  • while: पहले जाँच, शून्य बार भी चल सकता है (entry-controlled)।
  • do-while: बाद में जाँच, हमेशा कम से कम एक बार चलता है (exit-controlled), semicolon ध्यान से।
  • for (init; condition; update): पता पुनरावृत्ति के लिए गिनती वाला loop।
  • Killers: for(...) के बाद semicolon, छूटा update (infinite loop), off-by-one सीमाएँ।
  • Running-total accumulator ज़िंदगी भर का loop pattern है।
  • याद रखने का hook: guard पहले जाँचता है (while), पहले चखो (do-while), attendance गिनती (for)।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Decision Making and Iterative statements

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati