INTRODUCTIONRemember 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!");
}
}
The output will be:
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!");
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("All performances completed! (Outer Loop exited)");
}
}
The output will be:
Performance starts! (Outer loop at 1st iteration)
Monkey jumped 1 time(s) (Inner loop at 1st iteration)
Monkey jumped 2 time(s) (Inner loop at 2nd iteration)
Monkey jumped 3 time(s) (Inner loop at 3srd iteration)
Monkey jumped 4 time(s) (Inner loop at 4th iteration)
Monkey jumped 5 time(s) (Inner loop at last iteration)
Performance ended! (Inner Loop exited)
Banana acquired: 1
Performance starts! (Outer loop at 2nd iteration)
Monkey jumped 1 time(s) (Inner loop at 1st iteration)
Monkey jumped 2 time(s) (Inner loop at 2nd iteration)
Monkey jumped 3 time(s) (Inner loop at 3rd iteration)
Monkey jumped 4 time(s) (Inner loop at 4th iteration)
Monkey jumped 5 time(s) (Inner loop at last iteration)
Performance ended! (Inner Loop exited)
Banana acquired: 2
Performance starts! (Outer loop at 3rd iteration)
Monkey jumped 1 time(s) (Inner loop at 1st iteration)
Monkey jumped 2 time(s) (Inner loop at 2nd iteration)
Monkey jumped 3 time(s) (Inner loop at 3rd iteration)
Monkey jumped 4 time(s) (Inner loop at 4th iteration)
Monkey jumped 5 time(s) (Inner loop at last iteration)
Performance ended! (Inner Loop exited)
Banana acquired: 3
Performance starts! (Outer loop at 4th iteration)
Monkey jumped 1 time(s) (Inner loop at 1st iteration)
Monkey jumped 2 time(s) (Inner loop at 2nd iteration)
Monkey jumped 3 time(s) (Inner loop at 3rd iteration)
Monkey jumped 4 time(s) (Inner loop at 4th iteration)
Monkey jumped 5 time(s) (Inner loop at last iteration)
Performance ended! (Inner Loop exited)
Banana acquired: 4
Performance starts! (Outer loop at last iteration)
Monkey jumped 1 time(s) (Inner loop at 1st iteration)
Monkey jumped 2 time(s) (Inner loop at 2nd iteration)
Monkey jumped 3 time(s) (Inner loop at 3rd iteration)
Monkey jumped 4 time(s) (Inner loop at 4th iteration)
Monkey jumped 5 time(s) (Inner loop at last iteration)