Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › CONTROL FLOW
The square task for n = 4 prints four rows, and every row contains four stars. The outer loop selects one of the four rows. For each selected row, the inner loop prints exactly four stars before the next row begins.
****
****
****
****int n = 4;
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
cout << '*';
}
cout << '\n';
}int n = 4;
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
System.out.print('*');
}
System.out.println();
}For the first square row, the inner loop prints a star four times: star, star, star, star. Only after those four prints does the newline run once, moving output to the next row. The same sequence repeats for each of the four outer-loop passes.
star star star star newline
****
star star star star newline
****The newline belongs after the inner loop but inside the outer loop. If it runs inside the inner loop, every star gets its own line, producing 16 one-star lines. If it runs after the outer loop, all 16 stars are printed on one line, followed by one newline. Both mistakes keep the total number of stars but destroy the square's rows.
Which placement produces four lines of four stars for n = 4?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The rhombus uses the same four rows and the same four stars on every row as the square. Its difference is the number of leading spaces before those stars. For 0-based row r, the space count is n - 1 - r, so the four rows begin with 3, 2, 1 and 0 spaces.
****
****
****
****int n = 4;
for (int r = 0; r < n; r++) {
for (int s = 0; s < n - 1 - r; s++) {
cout << ' ';
}
for (int c = 0; c < n; c++) {
cout << '*';
}
cout << '\n';
}int n = 4;
for (int r = 0; r < n; r++) {
for (int s = 0; s < n - 1 - r; s++) {
System.out.print(' ');
}
for (int c = 0; c < n; c++) {
System.out.print('*');
}
System.out.println();
}At row 0, the space loop runs 3 times and the star loop runs 4 times, giving three spaces followed by four stars. At row 3, the space loop runs 0 times and the star loop still runs 4 times, so the row starts at the left edge without becoming narrower. Changing the space count shifts the block, but changing the star loop would change its width.
Complete the loop condition so the leading-space counts are 3, 2, 1, 0 for 0-based row r.
for (int s = 0; s < __________; s++) cout << ' ';Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The two inner loops control different properties. The space condition s < n - 1 - r controls where the four-star block starts. The star condition c < n controls how wide that block is. Keeping these jobs separate makes the running output predictable on every 0-based row.
| 0-BASED ROW R | SPACE CONDITION PASSES | STAR CONDITION PASSES | VISIBLE ROW |
|---|---|---|---|
| 0 | s < 3, so 3 spaces | c < 4, so 4 stars | **** |
| 1 | s < 2, so 2 spaces | c < 4, so 4 stars | **** |
| 2 | s < 1, so 1 space | c < 4, so 4 stars | **** |
| 3 | s < 0, so 0 spaces | c < 4, so 4 stars | **** |
An extra space pass gives a row one additional leading space, so that star block starts too far right. A missing space pass moves it too far left. The star loop has a separate boundary: c < n gives four stars, while c <= n gives five and c < n - 1 gives three. Either star mistake changes the rhombus width instead of its shift.