🟡 Control Flow  ·  Lesson 19

Nested Loops and Pattern Programs

What are nested loops?

A nested loop is simply a loop inside another loop. For every single pass of the outer loop, the inner loop runs all the way through. This two-level counting is exactly what you need to print patterns.

Thinking in rows and columns

The trick to every pattern is this: the outer loop is the row, and the inner loop is the column (what to print across that row). Decide how many rows you want, then decide what each row should contain.

A square of stars

Each of 3 rows prints 3 stars:

C++
for (int i = 1; i <= 3; i++) {       // rows
    for (int j = 1; j <= 3; j++) {   // columns
        cout << "* ";
    }
    cout << "\n";                     // new line after each row
}
Output:
* * *
* * *
* * *

A right triangle

Make the inner loop depend on the row number, so row i prints i stars:

C++
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++) {   // note: j <= i
        cout << "* ";
    }
    cout << "\n";
}
Output:
*
* *
* * *
* * * *

A number triangle

Print the column number instead of a star:

C++
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++) {
        cout << j << " ";
    }
    cout << "\n";
}
Output:
1
1 2
1 2 3
1 2 3 4

A pyramid

A pyramid adds a spaces loop before the stars — spaces shrink as stars grow:

C++
int rows = 4;
for (int i = 1; i <= rows; i++) {
    for (int s = 1; s <= rows - i; s++) cout << " ";   // spaces
    for (int j = 1; j <= 2 * i - 1; j++) cout << "*";  // stars
    cout << "\n";
}
Output:
   *
  ***
 *****
*******

Common mistakes

  • Forgetting the cout << "\n"; after each row, so everything prints on one line.
  • Using j <= 3 (fixed) when you meant j <= i (grows with the row).
  • Getting the spaces count wrong in a pyramid — it is rows - i.
  • Reusing the same variable name for both loops, breaking the counting.
🏋️ Practice

Print an inverted right triangle (4 stars on top, 1 at the bottom). Then try a number pyramid. Change one loop bound at a time and watch how the shape changes.

Summary

  • A nested loop runs the inner loop fully for each outer pass.
  • For patterns: outer loop = rows, inner loop = what fills each row.
  • Make the inner bound depend on i to get triangles.
  • Pyramids add a spaces loop before the stars.
  • Always print a newline at the end of each row.

Nested loops क्या हैं?

Nested loop बस एक loop के अंदर दूसरा loop है। बाहरी loop के हर एक pass के लिए, भीतरी loop पूरा चलता है। यही दो-स्तरीय गिनती patterns print करने के लिए चाहिए।

Rows और columns में सोचना

हर pattern की तरकीब यह है: बाहरी loop row है, और भीतरी loop column है (उस row में क्या print करना है)। पहले तय करें कितनी rows चाहिए, फिर तय करें हर row में क्या हो।

Stars का square

3 rows में से हर एक 3 stars print करती है:

C++
for (int i = 1; i <= 3; i++) {       // rows
    for (int j = 1; j <= 3; j++) {   // columns
        cout << "* ";
    }
    cout << "\n";                     // har row ke baad nai line
}
Output:
* * *
* * *
* * *

एक समकोण त्रिभुज

भीतरी loop को row number पर निर्भर बनाएँ, ताकि row i i stars print करे:

C++
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++) {   // dhyan: j <= i
        cout << "* ";
    }
    cout << "\n";
}
Output:
*
* *
* * *
* * * *

एक number त्रिभुज

Star की जगह column number print करें:

C++
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++) {
        cout << j << " ";
    }
    cout << "\n";
}
Output:
1
1 2
1 2 3
1 2 3 4

एक pyramid

Pyramid stars से पहले एक spaces loop जोड़ता है — stars बढ़ने पर spaces घटते हैं:

C++
int rows = 4;
for (int i = 1; i <= rows; i++) {
    for (int s = 1; s <= rows - i; s++) cout << " ";   // spaces
    for (int j = 1; j <= 2 * i - 1; j++) cout << "*";  // stars
    cout << "\n";
}
Output:
   *
  ***
 *****
*******

आम गलतियाँ

  • हर row के बाद cout << "\n"; भूलना, तो सब कुछ एक line पर print होता है।
  • j <= i (row के साथ बढ़ता) की जगह j <= 3 (निश्चित) इस्तेमाल करना।
  • Pyramid में spaces की गिनती गलत करना — यह rows - i है।
  • दोनों loops के लिए वही variable नाम दोबारा इस्तेमाल करना, गिनती तोड़ना।
🏋️ अभ्यास

एक उल्टा समकोण त्रिभुज print करें (ऊपर 4 stars, नीचे 1)। फिर एक number pyramid आज़माएँ। एक बार में एक loop bound बदलें और देखें आकार कैसे बदलता है।

सारांश

  • Nested loop हर बाहरी pass के लिए भीतरी loop पूरा चलाता है।
  • Patterns के लिए: बाहरी loop = rows, भीतरी loop = हर row में क्या भरे।
  • त्रिभुज पाने को भीतरी bound को i पर निर्भर बनाएँ।
  • Pyramids stars से पहले एक spaces loop जोड़ते हैं।
  • हर row के अंत में हमेशा एक newline print करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में nested loop क्या है?
Nested loop एक loop के अंदर रखा दूसरा loop है। भीतरी loop बाहरी loop के हर एक pass के लिए अपने सभी passes पूरे करता है, इसीलिए nested loops grids, tables और patterns के लिए इस्तेमाल होते हैं जहाँ दो स्तर की गिनती चाहिए।
C++ में patterns कैसे print करते हैं?
Patterns nested loops से print होते हैं: एक बाहरी loop rows नियंत्रित करता है और एक या ज़्यादा भीतरी loops हर row में जो print होता है उसे नियंत्रित करते हैं। भीतरी loop हर row में कितने stars, numbers या spaces print करे यह समायोजित करके आप त्रिभुज, pyramids और कई अन्य आकार बना सकते हैं।
Pattern programs के लिए nested loops क्यों इस्तेमाल होते हैं?
Patterns में दो आयाम होते हैं — rows, और हर row के भीतर items — तो आपको दो स्तर की looping चाहिए। बाहरी loop rows में नीचे जाता है जबकि भीतरी loop हर row भरता है, जो अधिकांश patterns के grid-जैसे आकार से स्वाभाविक रूप से मेल खाता है।
C++ में pyramid pattern कैसे print करें?
Pyramid में हर row को कुछ आगे के spaces फिर stars print करने होते हैं, spaces घटते और stars बढ़ते हुए जैसे आप नीचे जाते हैं। आप spaces के लिए एक भीतरी loop और stars के लिए दूसरा इस्तेमाल करते हैं, दोनों मौजूदा row number पर आधारित।
Nested loop की time complexity क्या है?
अगर बाहरी loop n बार चले और भीतरी loop हर एक के लिए n बार, body लगभग n × n बार चलती है, जो O(n²) है। यह छोटे patterns के लिए ठीक है पर तेज़ी से बढ़ता है, तो बड़े sizes पर गहराई से nested loops धीमे हो सकते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।