Remember when we talked about nested if-else statements ? One structure of if-else inside another structure. Similarly here we will have loop inside another loop. It'll come handy in many projects and actual coding later. A student who understands nested loops are often considered good in programming.
NESTED LOOPS
Suppose a monkey wants 5 bananas. For each banana it has to perform in circus. So it will be a loop of performing 5 times in circus. Now imagine the performance it gives in circus is jumping into a fire ring 5 times. This jumping into fire ring is also a loop of single act that it will do 5 times. So here we have a loop of working in circus. In each iteration it'll run another loop of jumping into the ring 5 times. In a single iteration of one loop, another loop runs and completes as many times as we set. This is the concept of nested loops.
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!");
}
}
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 simple one loop, now let's write a loop for the performance, i.e. in each performance monkey have 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)");
}
}
⦿ The outer loop runs 5 times → Once for each banana.
⦿ For each banana inner loop runs 5 times → 5 jumps
⦿ Total jumps = 5 x 5 = 25
⦿ In each iteration of outer loop, inner loop freshly starts from 1 and end at 5.
TECHNICAL EXAMPLE (Matrix)
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);
// Ask user for matrix size
System.out.print("Enter order of square matrix (e.g., 3 for 3x3): ");
int n = scanner.nextInt();
// Generate and print matrix
int value = 1; // start filling from 1for (int i = 1; i <= n; i++) {// rowsfor (int j = 1; j <= n; j++) {// columns System.out.print(value + "\t");
value++;
}
System.out.println();// move to next row}
scanner.close();
}
}
OUTPUT:
Enter order of square matrix (e.g., 3 for 3x3): 3
1 2 3
4 5 6
7 8 9
⦿ The outer loop controls number of rows. On each iteration it allows inner loop to run and do whatever it wants, at end of iteration it shifts to next line. And that's how it control rows.
⦿ The inner loop actually prints numbers on the row set by outer loop, it runs all of its iterations and print the numbers on single line.
⦿ Each value inner loop print act as separate colum on that row. Therefore we say inner loop controls number of columns.
⦿ Each time a value is printed value++; updates the value so that next value is NOT same as previous
⦿ The n in condition controls how many iteration outer loop and inner loop will run. If user enter 3, both inner and outer loop will run for 3 iterations. As result outer loop will create 3 rows, and inner loop will create 3 columns on it
CLOSING
That's it for Nested Loops in java. You will be now able to understand youtube tutorials and text books. There are 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 understand them from tutorials. For exercise and project try learning making the mentioned patterns. I would suggest learn from Apna College YT channel. Still if there is any issue, you can ask me or any other senior on campus. I assure you they are all really nice.