Pattern Manipulation

Lesson 10 of 20

Nested loops are printing presses: build squares, triangles and number pyramids one row and column at a time.

The big picture

Print with a printing press

Every pattern is rows of pieces. One loop prints the rows, a nested loop prints the pieces in each row.

Outer loop = rows

One pass of the outer loop produces one full line of the pattern.

Inner loop = columns

It prints the stars or numbers within the current row, then stops.

End each row

One "\n" after the inner loop finishes starts the next line.

The pattern trick

Make the inner bound depend on the row: j <= i prints i stars in row i.

Spaces count too

Pyramids print leading spaces — another inner loop places them.