By Dummy for Dummies
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.
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:
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!"); } }
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:
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)"); } }
Let's write a small code that outputs a square matrix of any order we want.
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(); } }
value++ updates the value so that the next value is NOT the same as the previous.n in the condition controls how many iterations the outer loop and inner loop will run. If the user enters 3, both inner and outer loops will run for 3 iterations. As a result, the outer loop will create 3 rows, and the inner loop will create 3 columns on each.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:
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.