Nested while, do-while and for loops

Nested loop में बाहर वाले loop के हर round पर अंदर वाला loop पूरा चलता है, इसलिए गिनतियाँ गुणा होती हैं।

5 min read · 10 cards · 5 checks

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


Theory

कितने stars?

एक loop दूसरे loop के अंदर बैठा है:

for (i = 1; i <= 3; i++)

for (j = 1; j <= 4; j++)

printf("*");

कितने stars दिखते हैं?

Think first

पहले आपकी गिनती

अपनी गिनती type कीजिए।

Show the answer

12: बाहर वाले 3 rounds में से हर एक के लिए अंदर वाला loop अपने सारे 4 rounds चलाता है, 3 × 4। गुणा कीजिए, जोड़िए मत: 7 कहना सबसे आम ग़लती है।

Theory

Loop के अंदर loop

कोई भी loop किसी दूसरे loop के अंदर बैठ सकता है: for, while या do-while। Nested loop में बाहर वाले loop के हर round पर अंदर वाला loop पूरा चलता है, जैसे हर घंटे पर मिनट की सुई पूरा एक चक्कर लगाती है। आम तौर पर बाहर वाला loop rows चलाता है और अंदर वाला columns, दोनों अपने-अपने counter के साथ, i और j।

Practical

Star वाला triangle

#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 4; i++) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

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

Think first

Pattern बताइए

ऊपर वाले program का output हूबहू type कीजिए या काग़ज़ पर बनाइए।

Show the answer

*

**

***

****

Row i में i stars आते हैं, क्योंकि अंदर वाला loop j <= i तक चलता है। वह सीमा बदलिए, pattern बदल जाएगा।

Quiz

Triangle वाले program में newline अंदर वाले loop के बाद, पर बाहर वाले loop के अंदर क्यों print होता है?

  1. वह हर row को, हर row पर एक बार, ख़त्म करता है
  2. सिर्फ़ style है; कहीं भी रखें, output वही रहेगा
  3. उसे हर star के लिए एक बार चलना है
  4. हर loop के अंत में newline ज़रूरी है
Show the answer

वह हर row को, हर row पर एक बार, ख़त्म करता है

Newline एक row को बंद करता है, इसलिए वह हर row पर एक बार चलता है। अंदर वाले loop में रखें तो हर star अपनी अलग line पर आएगा; दोनों loops के बाद रखें तो सारे 10 stars एक ही line पर आएँगे।

Think first

Bug पकड़िए

इसे 12 stars print करने चाहिए, पर यह सिर्फ़ 4 print करता है। क्यों?

for (i = 1; i <= 3; i++)

for (i = 1; i <= 4; i++)

printf("*");

Show the answer

दोनों loops i इस्तेमाल करते हैं। अंदर वाला loop i को 5 पर छोड़ता है, बाहर वाला update उसे 6 बना देता है, और 6 <= 3 false है, इसलिए 4 stars की सिर्फ़ एक row print होती है। दूसरा counter रखिए।

Practical

Exam-style: output बताइए

#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d", j);
        }
    }
    return 0;
}

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

Quiz

Exam-style: ऊपर वाले program का output क्या है?

  1. 112123
  2. 123123123
  3. 111222333
  4. 123
Show the answer

112123

Round 1 1 print करता है, round 2 12, round 3 123, और बीच में कोई newline नहीं है: 112123।

Summary

Key takeaways

  • बाहर वाले हर round पर अंदर वाला loop पूरा चलता है।
  • अंदर वाले loop की गिनती तय हो, तो कुल runs = बाहर × अंदर।
  • Rows के लिए बाहर वाला loop, columns के लिए अंदर वाला; newline अंदर वाले loop के बाद।
  • याद रखने का hook: 2 loops, 2 counters।

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

Nested while, do-while and for loops · Computer Programming and Programming Methodology (CPPM) · Gri-Learn