Watch nested loops print a star pyramid row by row: spaces first, then stars. Worked example with n = 5, pick any size from 3 to 8, and follow each loop step.
About this algorithm
The pyramid is a centred triangle: one star at the top, then 3, 5, 7 and so on, each row two stars wider and pushed in by the right number of leading spaces so the point sits in the middle. It is the first pattern where every row needs two numbers, the spaces before the stars and the stars themselves.
Try the pyramid once the right and inverted triangles feel easy. Those only change how many stars a row has; here you also have to work out where the stars begin, which is the skill behind the diamond and the hollow pyramid. Pick the inverted pyramid if you want the same shape pointing down, and the diamond when you want both halves stacked.
The bottom row has 2n - 1 stars, so the grid is n rows tall and 2n - 1 columns wide. The odd width is what gives the pyramid a single middle column for its tip.
Row i starts after n - 1 - i spaces. The top row is pushed in the most (n - 1 spaces) and the bottom row not at all.
Row i holds 2i + 1 stars: 1, 3, 5, and so on. On this page that is written as a column range: a star goes in every column j with spaces <= j <= spaces + 2i, and every other column is blank.
Leading spaces plus stars plus the spaces on the right make 2n - 1 on every row: (n - 1 - i) + (2i + 1) + (n - 1 - i) = 2n - 1. If your counts do not add up to the width, one of them is wrong.
The page opens with n = 5, so 5 rows and 9 columns. Row 0: 4 spaces, then 1 star in column 4. Row 1: 3 spaces, stars in columns 3 to 5. Row 2: 2 spaces, 5 stars in columns 2 to 6. Row 3: 1 space, 7 stars in columns 1 to 7. Row 4: no spaces, all 9 columns. The stars total 1 + 3 + 5 + 7 + 9 = 25, which is n², a neat check because the first n odd numbers always add up to n squared. The other 20 cells of the 45 cell grid are spaces.
Many solutions stop the row as soon as the last star is printed, so the right-hand spaces never appear. This page fills the whole width to show the shape is symmetric, which is why it visits 45 cells rather than 35.
Time: the grid is n rows by 2n - 1 columns, about 2n² cells, and each is visited once. Space: the spaces and star counts are worked out from i on the fly, so only a few integers are stored.