Pattern Matching Basics

Vaibhav • September 9, 2025

One of the most common exercises for mastering loops is pattern matching. By carefully controlling how loops run, we can print shapes like squares, triangles, and pyramids on the console. These exercises are not only fun, but also strengthen your understanding of for, while, and especially nested loops.

Pattern matching here means printing shapes using characters (usually *). Don’t confuse this with “pattern matching” in modern C# switch statements - that’s a different concept we’ll learn much later.

Starting simple: a straight line

Let’s begin with the easiest case: printing 5 stars in a row using a single for loop.

// print 5 stars in a row
for (int i = 1; i <= 5; i++)
{
    Console.Write("*");
}
Console.WriteLine();

Explanation: - The loop runs from 1 to 5. - Each time, Console.Write("*") prints one star. - After the loop finishes, Console.WriteLine() moves the cursor to the next line.

*****
    

Building a square

To make a square, we need two loops: one for rows, one for columns. The outer loop decides how many lines to print, while the inner loop prints stars on each line.

// 5x5 square
for (int r = 1; r <= 5; r++)
{
    for (int c = 1; c <= 5; c++)
    {
        Console.Write("*");
    }
    Console.WriteLine();
}

Explanation: - Outer loop (r) → controls the number of rows (5). - Inner loop (c) → prints 5 stars on each row. - After each row, Console.WriteLine() moves the cursor down.

*****
*****
*****
*****
*****
    

Right-angled triangle

We can use the row number to decide how many stars to print. Row 1 prints 1 star, row 2 prints 2, and so on.

// right-angled triangle
for (int r = 1; r <= 5; r++)
{
    for (int c = 1; c <= r; c++)
    {
        Console.Write("*");
    }
    Console.WriteLine();
}

Explanation: - On row 1, inner loop runs once → prints 1 star. - On row 2, inner loop runs twice → prints 2 stars. - This continues until row 5, which prints 5 stars.

*
**
***
****
*****
    

Inverted triangle

By reversing the loop, the number of stars decreases each row.

// inverted triangle
for (int r = 5; r >= 1; r--)
{
    for (int c = 1; c <= r; c++)
    {
        Console.Write("*");
    }
    Console.WriteLine();
}

Explanation: - First row: r = 5 → prints 5 stars. - Next row: r = 4 → prints 4 stars. - Continues until r = 1.

*****
****
***
**
*
    

Centered pyramid

Now let’s make a pyramid. Each row has spaces first, then stars. The trick is: - Number of spaces = rows - r. - Number of stars = 2 * r - 1.

// pyramid
int rows = 5;
for (int r = 1; r <= rows; r++)
{
    // print spaces
    for (int s = 1; s <= rows - r; s++)
    {
        Console.Write(" ");
    }
    // print stars
    for (int c = 1; c <= (2 * r - 1); c++)
    {
        Console.Write("*");
    }
    Console.WriteLine();
}

Explanation: - Row 1 → 4 spaces + 1 star. - Row 2 → 3 spaces + 3 stars. - Row 3 → 2 spaces + 5 stars. - Row 5 → 0 spaces + 9 stars. - This gives a centered pyramid shape.

    *
   ***
  *****
 *******
*********
    

If your pyramid looks slanted, check your spaces. Every row must add the right balance of spaces and stars.

Pattern printing is a classic interview warm-up question for beginners. It looks simple, but it tests how well you understand nested loops, counters, and using formulas inside loops.

Summary

Pattern matching with loops teaches you how to combine loops logically. By controlling rows, columns, spaces, and stars, you can create shapes like squares, triangles, and pyramids. These exercises may feel playful, but they give you a solid foundation in nested loops, which is essential for solving more complex problems later.