JAVA NESTED FOR LOOP

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]
  • What are Nested for loops in Java ?
  • Example :Nested For Loop in Java
  • Example :Java Pattern Program

What are Nested For loops in Java ?

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.

Syntax

for(statement; condition check; increment/decrement) {// Code inside outer loop 
for(statement; condition check; increment/decrement) {// Code inside inner loop }
// Code inside outer loop }

Note : In place of for loop you can use any loop. Nesting of loops can be done with any combination of loops i.e you can out for loop inside the body of while 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.

Example: Nested For Loop in Java

If you still dont know the implementation of a single for loop in java then kindly go through Java for loops tutorial.

Example 1: Nested for loop

copyimage
// 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");
             }
         }
    }
}
Enter the outer loop iteration value : 3Enter the inner loop iteration value : 2

OUTPUT :

  • Iteration : 1
  • Best Java Tutorials
  • Best Java Tutorials
  • Iteration : 2
  • Best Java Tutorials
  • Best Java Tutorials
  • Iteration : 3
  • Best Java Tutorials
  • Best Java Tutorials

Dry Run

Initializationouter-loopPrint outer loopinner-loopPrint inner loopIncr ++ /Decr --
i = 1; / j=1;1<=3 // trueIteration 11<=2 // trueBest Java Tutorialsi=1 ; / j= 2
i = 1; / j=2;2<=2 // trueBest Java Tutorialsi=1 ; / j= 3
i = 1; / j=3;3<=2 // falsei=2 ; / j= 1;
i = 2; / j=1;2<=3 // trueIteration 21<=2 // trueBest Java Tutorialsi=2 ; / j= 2
i = 2; / j=2;2<=2 // trueBest Java Tutorialsi=2 ; / j= 3
i = 2; / j=3;3<=2 // falsei=3 ; / j=1;
i = 3; / j=1;3<=3 // trueIteration 31<=2 // trueBest Java Tutorialsi=3 ; / j= 2
i = 3; / j=2;2<=2 // trueBest Java Tutorialsi=3 ; / j= 3
i = 3; / j=3;3<=2 // falsei=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.

Example: Java Pattern Program

In this example we will create a pattern with the help of Java nested for loops.

  • *
  • * *
  • * * *
  • * * * *
  • * * * * *

Example 1: Right Triangle Star Pattern

copyimage
// 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("");
            }
      }
}
Enter the height : 5

OUTPUT :

  • *
  • * *
  • * * *
  • * * * *
  • * * * * *

Dry Run

Initializationouter-loopinner-loopPrint inner loopPrint outer loopIncr ++ /Decr --
i=1; / j=11<=5 // true1<=1 // true*i=1 ; / j=2;
i=1; / j=22<=1 // falsenext linei=2 ; / j=1;
i=2; / j=12<=5 // true1<=2 // true*i=2 ; / j=2;
i=2; / j=22<=2 // true* *i=2 ; / j=3;
i=2; / j=33<=2 // falsenext linei=3 ; / j=1;
i=3; / j=13<=5 // true1<=3 // true*i=3 ; / j=2;
i=3; / j=22<=3 // true* *i=3 ; / j=3;
i=3; / j=33<=3 // true* * *i=3 ; / j=4;
i=3; / j=44<=3 // falsenext linei=4 ; / j=1;
i=4; / j=14<=5 // true1<=4 // true*i=4 ; / j=2;
i=4; / j=22<=4 // true* *i=4 ; / j=3;
i=4; / j=33<=4 // true* * *i=4 ; / j=4;
i=4; / j=44<=4 // true* * * *i=4 ; / j=1;
i=4; / j=55<=4 // falsenext linei=5 ; / j=1;
i=5; / j=14<=5 // true1<=5 // true*i=5 ; / j=2;
i=5; / j=22<=5 // true* *i=5 ; / j=3;
i=5; / j=33<=5 // true* * *i=5 ; / j=4;
i=5; / j=44<=5 // true* * * *i=5 ; / j=5;
i=5; / j=55<=5 // true* * * * *i=5 ; / j=6;
i=5; / j=66<=5 // falsenext linei=6 ; / j=1;
i=6; / j=66<=5 // false

Its time to learn while loop in Java . Switch on to the next tutorial.