🟡 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.

Frequently Asked Questions

What is a nested loop in C++?
A nested loop is a loop placed inside another loop. The inner loop completes all its passes for each single pass of the outer loop, which is why nested loops are used for grids, tables and patterns where you need two levels of counting.
How do you print patterns in C++?
Patterns are printed using nested loops: an outer loop controls the rows and one or more inner loops control what is printed in each row. By adjusting how many stars, numbers or spaces the inner loop prints per row, you can build triangles, pyramids and many other shapes.
Why are nested loops used for pattern programs?
Patterns have two dimensions — rows, and the items within each row — so you need two levels of looping. The outer loop moves down the rows while the inner loop fills each row, which maps naturally to the grid-like shape of most patterns.
How do you print a pyramid pattern in C++?
A pyramid needs each row to print some leading spaces followed by stars, with the spaces decreasing and the stars increasing as you go down. You use one inner loop for the spaces and another for the stars, both based on the current row number.
What is the time complexity of a nested loop?
If an outer loop runs n times and the inner loop runs n times for each, the body runs about n × n times, which is O(n²). This is fine for small patterns but grows quickly, so deeply nested loops over large sizes can become slow.

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 करें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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