Nested for loop in java is a way to place one loop inside the body of other loop where outer loop takes care of the number of complete repetitions of the inner loop. This is also known as nesting of loops
Table of Contents
[hide]Nested For Loop in Java consists of nested loops i.e one loop inside another. Once outer loop executes then execution of inner loop happens completely. These nested loops are very important in pattern programs and these pattern problems are one of the hot topic of interviews.
for(statement; condition check; increment/decrement) {// Code inside outer loopfor(statement; condition check; increment/decrement) {// Code inside inner loop }// Code inside outer loop }
As soon as you enter inside the body of outer loop, inner loop executes completely. Now in the next module we will see few examples related to nesting for for loops in Java.
If you still dont know the implementation of a single for loop in java then kindly go through Java for loops tutorial.
// Java program to demonstrate nested for loop example
import java.util.*;
public class NestedLoopExample{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter the outer loop iteration value : ");
int outer= sc.nextInt();
System.out.print("Enter the inner loop iteration value : ");
int inner= sc.nextInt();
for(int i = 1 ; i <= outer ; i++ ) {
System.out.println("Iteration : " + i);
for(int j = 1; j <= inner; j++) {
System.out.println("Best Java Tutorials");
}
}
}
}
Initialization | outer-loop | Print outer loop | inner-loop | Print inner loop | Incr ++ /Decr -- |
i = 1; / j=1; | 1<=3 // true | Iteration 1 | 1<=2 // true | Best Java Tutorials | i=1 ; / j= 2 |
i = 1; / j=2; | 2<=2 // true | Best Java Tutorials | i=1 ; / j= 3 | ||
i = 1; / j=3; | 3<=2 // false | i=2 ; / j= 1; | |||
i = 2; / j=1; | 2<=3 // true | Iteration 2 | 1<=2 // true | Best Java Tutorials | i=2 ; / j= 2 |
i = 2; / j=2; | 2<=2 // true | Best Java Tutorials | i=2 ; / j= 3 | ||
i = 2; / j=3; | 3<=2 // false | i=3 ; / j=1; | |||
i = 3; / j=1; | 3<=3 // true | Iteration 3 | 1<=2 // true | Best Java Tutorials | i=3 ; / j= 2 |
i = 3; / j=2; | 2<=2 // true | Best Java Tutorials | i=3 ; / j= 3 | ||
i = 3; / j=3; | 3<=2 // false | i=4 ; / j= 1 | |||
i = 4; / j=1; | 4<=3 //false |
In this example we have taken the inputs as 3 and 2. Kindly give different values for i and j and then dry run your program with pen and paper to understand the flow.
In this example we will create a pattern with the help of Java nested for loops.
// Java program to create a Right Triangle Star Pattern Program
import java.util.*;
public class NestedLoopExample{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter the height : ");
int height= sc.nextInt();
for(int i = 1 ; i <= height ; i++ ) {
for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
Initialization | outer-loop | inner-loop | Print inner loop | Print outer loop | Incr ++ /Decr -- |
i=1; / j=1 | 1<=5 // true | 1<=1 // true | * | i=1 ; / j=2; | |
i=1; / j=2 | 2<=1 // false | next line | i=2 ; / j=1; | ||
i=2; / j=1 | 2<=5 // true | 1<=2 // true | * | i=2 ; / j=2; | |
i=2; / j=2 | 2<=2 // true | * * | i=2 ; / j=3; | ||
i=2; / j=3 | 3<=2 // false | next line | i=3 ; / j=1; | ||
i=3; / j=1 | 3<=5 // true | 1<=3 // true | * | i=3 ; / j=2; | |
i=3; / j=2 | 2<=3 // true | * * | i=3 ; / j=3; | ||
i=3; / j=3 | 3<=3 // true | * * * | i=3 ; / j=4; | ||
i=3; / j=4 | 4<=3 // false | next line | i=4 ; / j=1; | ||
i=4; / j=1 | 4<=5 // true | 1<=4 // true | * | i=4 ; / j=2; | |
i=4; / j=2 | 2<=4 // true | * * | i=4 ; / j=3; | ||
i=4; / j=3 | 3<=4 // true | * * * | i=4 ; / j=4; | ||
i=4; / j=4 | 4<=4 // true | * * * * | i=4 ; / j=1; | ||
i=4; / j=5 | 5<=4 // false | next line | i=5 ; / j=1; | ||
i=5; / j=1 | 4<=5 // true | 1<=5 // true | * | i=5 ; / j=2; | |
i=5; / j=2 | 2<=5 // true | * * | i=5 ; / j=3; | ||
i=5; / j=3 | 3<=5 // true | * * * | i=5 ; / j=4; | ||
i=5; / j=4 | 4<=5 // true | * * * * | i=5 ; / j=5; | ||
i=5; / j=5 | 5<=5 // true | * * * * * | i=5 ; / j=6; | ||
i=5; / j=6 | 6<=5 // false | next line | i=6 ; / j=1; | ||
i=6; / j=6 | 6<=5 // false |
Its time to learn while loop in Java . Switch on to the next tutorial.