Nested Loops in Java: Creating Patterns and Designs
A Simple Thought
Think about how rows and columns work together.
A classroom has rows of seats, a calendar has rows of days, and a chessboard has rows and columns of squares.
In programming, you can create similar patterns using loops inside loops.
This idea is called a nested loop.
In this lesson, you will learn how to use nested loops to print patterns made of stars and numbers.
You will create a simple triangle pattern on the screen, just like building blocks stacked in order.
The Project: Star Triangle Pattern
Your program will print a triangle made of stars, starting with one star in the first row, two in the next, and so on.
The output should look like this:
1 2 3 4 5 6 7 8======================== STAR TRIANGLE * ** *** **** ***** ========================
Each row adds one more star than the previous one. You will use two loops to make this pattern appear.
The Concept: Nested Loops
A nested loop means one loop running inside another.
The outer loop controls the number of rows, while the inner loop controls what happens inside each row.
Here’s a simple example:
1 2 3 4 5 6for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
In this program:
- The outer loop (i) runs from 1 to 5, creating five rows.
- The inner loop (j) prints stars.
- The number of stars in each row depends on the current value of i.
When i is 1, one star prints. When i is 2, two stars print, and so on.
Why This Matters
Nested loops help you create structured, repeating patterns.
They are used in many real-world applications:
- Printing tables and grids
- Designing simple console-based games
- Handling multi-dimensional data such as spreadsheets or matrices
Without nested loops, you would have to write separate loops for each line or each column, which would make programs long and repetitive.
Let’s Build the Star Triangle
Here’s the complete program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16public class StarPattern { public static void main(String[] args) { System.out.println("========================"); System.out.println(" STAR TRIANGLE"); for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } System.out.println("========================"); } }
When you run this, the stars form a growing triangle.
The inner loop runs several times for each single run of the outer loop, producing one full row before moving to the next.
How It Works
- The outer loop controls how many rows are printed.
- The inner loop decides how many stars appear in each row.
- After the inner loop finishes one row, System.out.println() moves the cursor to the next line.
- As i increases, the number of stars also increases, forming the triangle pattern.
This is how nested loops create complex output step by step.
Learn Together with AI
If you are using an AI Copilot, you can describe what kind of pattern you want to build and let it generate the code for you.
Try this prompt:
“Write a Java program that prints a triangle made of stars using nested loops.”
After you test it, ask for changes:
- “Make it print numbers instead of stars.”
- “Turn the triangle upside down.”
- “Create a square of 5 rows and 5 columns.”
You will see how small adjustments to the loops can completely change the pattern.
Practice Challenge
Create your own program that prints a square of stars, 5 rows and 5 columns.
The output should look like this:
1 2 3 4 5 6 7 8======================== STAR SQUARE ***** ***** ***** ***** ***** ========================
Use nested loops to print each row and column.
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16public class StarSquare { public static void main(String[] args) { System.out.println("========================"); System.out.println(" STAR SQUARE"); for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { System.out.print("*"); } System.out.println(); } System.out.println("========================"); } }
This program uses one loop for rows and another for columns. The result is a clean, even square pattern printed line by line.
Coming Up Next
You’ve learned how to build shapes and repeat patterns using nested loops.
Next, you will move from static data to collections, groups of items stored together.
You will learn how to use arrays in Java to keep lists of information such as names, scores, or products.