← Back to all posts

11 Java Fundamentals

Nested Loops in Java

By Dummy for Dummies


Introduction

Remember nested if-else statements? One structure sitting inside another. Same idea here, except now it's a loop inside a loop. You'll run into this constantly once you start building real programs, so it's worth getting comfortable with early.


Nested Loops

Suppose a monkey wants 5 bananas. For each banana, it has to perform in the circus, so that's a loop running 5 times. Now say the performance itself is jumping through a fire ring, and it does that 5 times per performance too. That jump is its own loop, nested inside the performance loop.

So the outer loop handles the circus performances. Each time it runs once, the inner loop runs its full 5 jumps before the outer loop moves to the next performance. That's nested loops: one loop completing in full, inside a single step of another.

Here is a demonstration of just one loop, monkey performing and getting banana:

Java
public class Main {
    public static void main(String[] args) {
        // Monkey performing and getting banana
        for (int banana = 1; banana < 6; banana++) {
            System.out.println("Monkey performed in circus");
            System.out.println("Banana acquired: " + banana);
        }
        System.out.println("Loop exited!");
    }
}
Console Output
Monkey performed in circus Banana acquired: 1 Monkey performed in circus Banana acquired: 2 Monkey performed in circus Banana acquired: 3 Monkey performed in circus Banana acquired: 4 Monkey performed in circus Banana acquired: 5 Loop exited!

This is a simple one loop.


Now let's write a loop for the performance, i.e., in each performance the monkey has to jump into the ring 5 times, so we will replace the performance line of code with a loop:

Java
public class Main {
    public static void main(String[] args) {
        // Monkey performing and getting banana
        for (int banana = 1; banana < 6; banana++) {
            System.out.println("Performance starts!");

            // Inner loop: monkey jumps 1 to 5 times
            for (int jump = 1; jump < 6; jump++) {
                System.out.println("Monkey jumped " + jump + " time(s)");
            }

            System.out.println("Performance ended! (Inner Loop exited!)");
            System.out.println("Banana acquired: " + banana);
            System.out.println();
        }
        System.out.println("All performances completed! (Outer Loop exited)");
    }
}
Console Output
Performance starts! Monkey jumped 1 time(s) Monkey jumped 2 time(s) Monkey jumped 3 time(s) Monkey jumped 4 time(s) Monkey jumped 5 time(s) Performance ended! (Inner Loop exited!) Banana acquired: 1 Performance starts! Monkey jumped 1 time(s) Monkey jumped 2 time(s) Monkey jumped 3 time(s) Monkey jumped 4 time(s) Monkey jumped 5 time(s) Performance ended! (Inner Loop exited!) Banana acquired: 2 Performance starts! Monkey jumped 1 time(s) Monkey jumped 2 time(s) Monkey jumped 3 time(s) Monkey jumped 4 time(s) Monkey jumped 5 time(s) Performance ended! (Inner Loop exited!) Banana acquired: 3 Performance starts! Monkey jumped 1 time(s) Monkey jumped 2 time(s) Monkey jumped 3 time(s) Monkey jumped 4 time(s) Monkey jumped 5 time(s) Performance ended! (Inner Loop exited!) Banana acquired: 4 Performance starts! Monkey jumped 1 time(s) Monkey jumped 2 time(s) Monkey jumped 3 time(s) Monkey jumped 4 time(s) Monkey jumped 5 time(s) Performance ended! (Inner Loop exited!) Banana acquired: 5 All performances completed! (Outer Loop exited)

Technical Example (Matrix)

Let's write a small code that outputs a square matrix of any order we want.

Java
import java.util.Scanner;

public class SquareMatrix {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter order of square matrix (e.g., 3 for 3x3): ");
        int n = scanner.nextInt();

        int value = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.print(value + "\t");
                value++;
            }
            System.out.println();
        }

        scanner.close();
    }
}
Console Output
Enter order of square matrix (e.g., 3 for 3x3): 3 1 2 3 4 5 6 7 8 9

Closing

That's it for Nested Loops in Java. You will now be able to understand YouTube tutorials and textbooks. There are a few complex things in nested loops though, e.g., pattern making like this:

Right Triangle: * * * * * * * * * * * * * * * Pyramid: * * * * * * * * * * * * * * * * * * * * * * * * * Hollow Triangle: * * * * * * * * * * * * Hollow Square: * * * * * * * * * * * * * * * *

But now you'll be able to make sense of them from tutorials. For practice, try building the patterns above yourself. I'd suggest the Apna College YouTube channel for this. And if you get stuck, ask me or any senior on campus, they're all genuinely helpful.

← Previous 10 - For Loop in Java Next → 12 - Methods in Java