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:
for (int i = 1; i <= 3; i++) { // rows
for (int j = 1; j <= 3; j++) { // columns
cout << "* ";
}
cout << "\n"; // new line after each row
}* * *
* * *
* * *
A right triangle
Make the inner loop depend on the row number, so row i prints i stars:
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) { // note: j <= i
cout << "* ";
}
cout << "\n";
}*
* *
* * *
* * * *
A number triangle
Print the column number instead of a star:
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << "\n";
}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:
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";
}*
***
*****
*******
Common mistakes
- Forgetting the
cout << "\n";after each row, so everything prints on one line. - Using
j <= 3(fixed) when you meantj <= 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.
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
ito 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++?
How do you print patterns in C++?
Why are nested loops used for pattern programs?
How do you print a pyramid pattern in C++?
What is the time complexity of a nested loop?
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 करती है:
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
}* * *
* * *
* * *
एक समकोण त्रिभुज
भीतरी loop को row number पर निर्भर बनाएँ, ताकि row i i stars print करे:
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) { // dhyan: j <= i
cout << "* ";
}
cout << "\n";
}*
* *
* * *
* * * *
एक number त्रिभुज
Star की जगह column number print करें:
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << "\n";
}1
1 2
1 2 3
1 2 3 4
एक pyramid
Pyramid stars से पहले एक spaces loop जोड़ता है — stars बढ़ने पर spaces घटते हैं:
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";
}*
***
*****
*******
आम गलतियाँ
- हर 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 करें।