Print a right-angled triangle of stars with an outer loop for the rows and an inner loop for the stars, then trace it row by row
Pattern printing is a classic beginner exercise and a common fresher interview question. In this lesson we print a star pattern using nested loops: a right-angled triangle with 1 star in the first row and 4 stars in the fourth.
The trick that makes every pattern easier: one loop for the rows, one loop for the stars in a row, and a clear relation between the two.
The pattern we want
*
* *
* * *
* * * *
Row 1 has 1 star, row 2 has 2 stars, and so on up to row 4. Count the stars in a row and you get the row number: that observation is the whole solution.
Star pattern using nested loops: why two loops?
One loop can print stars in a single line. But we need several lines, and a different number of stars on each. So we use two loops, one inside the other (nested loops):
- the outer loop (OL) goes through the rows, 1 to 4;
- the inner loop (IL) prints the stars of the current row, then the program moves to the next line.
The nested loop flowchart, step by step
- Start.
- OL = 1: start at the first row.
- OL <= 4? (decision): if no, End.
- IL = 1: start counting stars in this row.
- IL <= OL? (decision): a row prints as many stars as its row number.
- Yes: print \*, then IL = IL + 1, and go back to the IL decision.
- No: move to the next line, then OL = OL + 1, and go back to the OL decision.

Dry run, row by row
| OL | IL values that print | Stars | Then | |—|—|—|—| | 1 | 1 | * | IL = 2 > 1: next line | | 2 | 1, 2 | * * | IL = 3 > 2: next line | | 3 | 1, 2, 3 | * * * | IL = 4 > 3: next line | | 4 | 1, 2, 3, 4 | * * * * | IL = 5 > 4: next line | | 5 | – | – | 5 <= 4 is false: End |

The inner loop restarts at 1 on every row, and it runs exactly OL times.
The trick for any pattern
Whenever you meet a new pattern, write down how the inner loop relates to the outer loop. Here it is IL runs OL times (IL <= OL). For an upside-down triangle it becomes ‘IL runs 5 − OL times’. Find that small formula first, and the flowchart (and later the code) follows.
From flowchart to code
Once the flowchart is clear, the code is mostly translation. In pseudo-code the same logic looks like this:
for OL = 1 to 4
for IL = 1 to OL
print "*"
print new line
The outer decision OL <= 4 becomes the outer loop, the inner decision IL <= OL becomes the inner loop, and the ‘next line’ box becomes the new-line print after the inner loop. Every programming language you learn later (C, C#, Java or Python) writes this with the same two nested loops; only the syntax changes.
Video chapters
- 0:00 Pattern printing: an interview favourite
- 0:48 The pattern: a star triangle
- 1:05 Two loops: outer for rows, inner for stars
- 2:11 Drawing the flowchart
- 3:59 Dry run, row by row
- 6:06 The trick: relate the inner loop to the outer loop
- 6:57 Wrap-up
Watch the short versions
Short on time? These Shorts from this lesson each make one point in under a minute.
- Star Pattern: Why You Need Two Loops (21 s Short, coming soon, from 1:09 in the full video)
- Dry Run: How the First Row Prints One Star (28 s Short, coming soon, from 4:25 in the full video)
- The Trick for Any Star Pattern (34 s Short, coming soon, from 6:05 in the full video)
Key takeaways
- A star pattern needs nested loops: an outer loop for rows and an inner loop for stars.
- The outer loop runs OL = 1 to 4 (OL <= 4).
- The inner loop restarts at 1 on every row and runs while IL <= OL.
- After each row: move to the next line and add 1 to OL.
- For any pattern, find the relation between the inner loop and the outer loop first.
Frequently asked questions
Why do star patterns need nested loops?
One loop handles the rows and the other prints the stars in each row. A single loop can only repeat one thing in one line.
What does IL <= OL mean in the flowchart?
The inner loop keeps printing stars while its counter is at most the current row number, so row 1 gets 1 star and row 4 gets 4.
How do I print 5 rows instead of 4?
Change the outer condition to OL <= 5. The inner condition IL <= OL stays the same.
Further reading
- C program to print right triangle star pattern (BeginnersBook): the same pattern in C code
- Nested loops (K-State CIS 200 textbook): rows and columns with nested loops
Practice: change the flowchart to print the upside-down triangle (4 stars first, 1 star last). What changes: the outer loop or the inner loop’s condition?
Next in the series: more complex star patterns. Watch the video, subscribe to the channel and follow along.
