DSA FUNDAMENTALS › CONTROL FLOW
For n = 4, the triangle has four output lines. The first line has one star, the second has two, the third has three, and the fourth has four.
*
**
***
****The row number tells you the star count directly: row 1 has 1 star, row 2 has 2 stars, row 3 has 3 stars, and row 4 has 4 stars. The rule is row r -> r stars. The nested loop must use this changing row value to create the stair-step shape.
The outer loop visits rows 1 through 4. For each row, the inner loop starts col at 1 and prints stars while col is at most row. Because the loop condition uses row, its number of passes changes from one row to the next.
int n = 4;
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
cout << "*";
}
cout << '\n';
}When row = 3, the inner loop uses col values 1, 2, and 3, then stops. If its condition were col <= n, it would use col values 1, 2, 3, and 4 on every row. That would print four stars on all four rows, producing a square instead of a triangle.
Which inner-loop condition prints exactly three stars when row = 3 in the n = 4 example?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Star printing belongs inside the inner loop because each pass prints one star. Newline printing belongs immediately after the inner loop, because that is when the current row is complete. For row 3, three inner-loop passes print three stars, then one newline moves output to the next line.
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= row; col++) {
cout << "*";
}
cout << '\n';
}A newline inside the inner loop would move to a new line after every star, so the output would contain one star per line. A newline after the outer loop would wait until all ten stars had been printed, so the stars would appear on one line. Exactly one newline is needed after each completed row.
Move or rewrite the newline statement so the output remains four lines with 1, 2, 3, and 4 stars.
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= row; col++) {
cout << "*";
cout << '\n';
}
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The inner-loop counts for rows 1 through 4 are 1, 2, 3, and 4. Adding them gives 1 + 2 + 3 + 4 = 10 star prints. The newline after each inner loop preserves the four row boundaries, so those ten prints appear as the required triangle rather than as one uninterrupted line.
| ROW | COL VALUES USED | STARS PRINTED | NEWLINE |
|---|---|---|---|
| 1 | 1 | 1 | once after the inner loop |
| 2 | 1, 2 | 2 | once after the inner loop |
| 3 | 1, 2, 3 | 3 | once after the inner loop |
| 4 | 1, 2, 3, 4 | 4 | once after the inner loop |
| Total | 10 star prints | 10 | 4 newlines |
The same row-star rule and newline placement work in both C++ and Java. In each implementation, row runs from 1 through 4, col runs only through row, and the newline runs once after the inner loop.
int n = 4;
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
cout << "*";
}
cout << '\n';
}int n = 4;
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print("*");
}
System.out.println();
}