C & C++ fundamentals / 09

Nested loops and star patterns

Use increasing and decreasing counters, nested loops, and spacing to print triangle and pyramid patterns.

On this page

In the last lesson, we covered basic loops. Now let's explore some variations and applications.

A loop without a condition

A for loop can omit its condition: for (int i = 1; ; i++) {}. Like while (1), this has no condition that automatically ends it. You need an exit such as break; to stop it.

Editorial note: an indefinitely incrementing signed integer can eventually overflow. This introductory pattern is not a recommendation to let that counter grow without a bound.

An increasing triangle

Let's nest loops to print rows containing one star, then two, then three:

#include <stdio.h>

int main()
{
    int num;

    scanf("%d",&num);

    for(int i=1;i<=num;i++)
    {
        for(int j = 1;j <= i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
}

The program reads a value into num and increases i up to that value. Think of i as the row and j as the position within that row. When i is 1, the inner loop prints one star; when i is 2, it prints two. A newline ends each row before the next iteration begins.

A decreasing triangle

An update expression can decrease a counter instead. This is useful when moving from a larger value to a smaller one:

#include <stdio.h>

int main()
{
    int num;

    scanf("%d",&num);

    for(int i=num;i>=1;i--)
    {
        for(int j = i;j >= 1; j--)
        {
            printf("*");
        }
        printf("\n");
    }
}

With input 3, the program prints three stars on the first row, two on the second, and one on the third. Increasing and decreasing update expressions give us opposite directions of iteration. A for loop is often convenient when the counter's progression is explicit.

A centered pyramid

Now let's add spaces before the stars to create a centered pattern:

#include <stdio.h>

int main()
{
    int num;

    scanf("%d",&num);

    
    for(int i=1;i<=num;i++)
    {
        for(int j=1;j<=num - i;j++)
        {
            printf(" ");
        }

        for(int j=1;j <= i * 2 - 1;j++)
        {
            printf("*");
        }
        printf("\n");   
    }
    return 0;
}

The first inner loop prints spaces. Its limit is num - i: if the input is four and i is initially one, the first row needs three spaces. As the row number increases, the number of leading spaces decreases.

The second inner loop prints 2 * i - 1 stars. That expression generates the odd counts 1, 3, 5, and 7, giving the pyramid its shape.

That is all for this lesson. The original closing preview mentioned structures; the next published entry in this archive instead begins arrays.

Moved here from Tistory

I migrated this post from my Korean Tistory blog, I am Jason Lee, to this website and translated it into English. My writing and projects now live together in one place.

The original publication date, code examples, and screenshots are preserved. Editorial notes clarify known issues in the original material.

Original post on Tistory English edition · Aug 30, 2026
← Back to all articles