Nested For Loop in Java | Pattern Example

Nested for loop in Java means one for statement inside another for statement. In other words, a for loop nested inside another for loop is called nested for loops.

A nested for loops consists of an outer for loop and one or more inner for loops. Each time the outer for loop repeats, the inner for loop re-enters and starts a new execution.

That is, each time the control will enter inside the inner for loop when the outer for loop repeats.

We can put many loops inside a loop. But, it is advice that you do not go beyond three levels of nested loops, as it will make the program look clumsy.

Syntax of Nested for loop in Java


The general syntax for using nested for loop in Java is as follows:

// Outer for loop.
for ( initialization; test-condition; increment ) 
{
// Inner for loop.
   for ( initialization; test-condition; increment ) 
   {
    // statement of inner loop
   }
 // statement of outer loop
}

Let’s understand it with the help of an example.

for(int i = 1; i <= 3; i++){
  statement1; // This statement will execute 3 times by changing i values from 1 to 3.
}
for(int j = 1; j <= 4; j++){
  statement2; // This statement will execute 4 times by changing j value from 1 to 4.
}

If we write first for loop inside the second for loop, it will look like this:

for(int i = 1; i <= 3; i++)
{
  statement1; // will execute 3 times.
  for(int j = 1; j <= 4; j++)
  {
    statement2; // will execute 12 (3 * 4) times.
  }
}

a) In this example, when i = 1, execution will start from the outer for loop and statement1 will execute once.

b) Now, the control of execution enters inside the inner for loop. Since we initially set the control variable j to 1, statement2 will execute once.

c) After this execution, the value of j will be 2 because of increment by 1. Then, statement2 will execute once again.

d) Like this, the inner for loop will execute 4 times with changing j values from 1 to 4. This means that statement2 will execute 4 times in the first execution of outer for loop.

e) When the execution of inner for loop completes, the control of execution goes to the outer for loop. Now, the value of i will be 2 because of increment by 1.

This time, the control of execution again enters inside the inner for loop and statement2 will execute 4 times.

f) Then, the control again goes to outer for loop and the value of i will set to 3. Again, the inner for loop will execute 4 times.

This means that the values of i and j will change as:

  • When i = 1,  j = 1, 2, 3, 4
  • i = 2,  j = 1, 2, 3, 4
  • i = 3,  j = 1, 2, 3, 4

In the above sequence, the outer for loop will execute total 3 times. Hence, statement1 inside the loop body will also execute 3 times.

Since the inner for loop will execute 4 times for each i value therefore, statements2 inside the loop body will execute 12 times.

In the same way, we can also nest a while loop or a do-while loop inside a for loop and vice versa. These are called nested loops in Java.

Flowchart Diagram for Nested Loops in Java


The basic flowchart diagram for nested loops in Java has shown in the below figure.

Nested for loop in Java

Thus, we can also nest a while loop or a do-while loop inside a for loop and vice versa. These are called nested loops in Java.

The general syntax to nest while loop inside a for loop is as:

for(initialization; test-condition; increment/decrement)
{
  statements;
  while(conditional expression)
  {
    statements;
  }
}

Example Program based on Nested for loops


Let’s write a program in which we will display values of the inner for loop for each outer iteration, as well as outer for loop.

Program code 1:

public class NestedForLoopEx {
public static void main(String[] args) 
{
// Outer for loop.
  for(int i = 1; i <= 3; i++)
  {
    System.out.print(i+ "\n"); // will execute 3 times.
 // Inner for loop.
    for(int j = 1; j <= 4; j++){
      System.out.print(j+ " "); // will execute 12 (3 * 4) times.
    }
    System.out.println(" ");
   }
  }
}
Output:
     1
     1 2 3 4  
     2
     1 2 3 4  
     3
     1 2 3 4 

As you observe in the output, during each iteration of outer for loop, the statement inside the inner for loop executes 4 times. Thus, outer for loop executes three times and inner for loop executes total four times.


2. Let’s write Java program to display tables for 1 and 2 using nested for loops.

Program code 2:

public class Tables {
public static void main(String[] args) 
{
System.out.println("Displaying Tables: ");	
// Outer for loop.
  for(int i = 1; i <= 3; i++)
  {
 // Inner for loop.
   for(int j = 1; j <= 10; j++){
     System.out.println(i+ " * " +j+" = "+ (i*j));
    }
   System.out.println(" ");
  }
 }
}
Output:
     Displaying Tables: 
     1 * 1 = 1
     1 * 2 = 2
     1 * 3 = 3
     1 * 4 = 4
     1 * 5 = 5
     1 * 6 = 6
     1 * 7 = 7
     1 * 8 = 8
     1 * 9 = 9
     1 * 10 = 10
 
     2 * 1 = 2
     2 * 2 = 4
     2 * 3 = 6
     2 * 4 = 8
     2 * 5 = 10
     2 * 6 = 12
     2 * 7 = 14
     2 * 8 = 16
     2 * 9 = 18
     2 * 10 = 20

Program based on Java Pattern using Nested for loop


1. Let’s write Java program to display triangle of * using nested for loop.

Program code 3:

public class PatternEx {
public static void main(String[] args) 
{
System.out.println("Displaying right triangle of *: ");	
// Outer for loop.
  for(int i = 1; i <= 5; i++)
  {
 // Inner for loop.
	for(int j = 1; j <= i; j++){
     System.out.print("*"+ " ");
    }
   System.out.println(" ");
  }
 }
}
Output:
     Displaying right triangle of *: 
     * 
     * * 
     * * * 
     * * * * 
     * * * * * 

2. Let’s create a Java program to display a triangle of numbers.

Program code 4:

public class PatternEx {
public static void main(String[] args) 
{
System.out.println("Displaying right triangle pattern of numbers: ");	
// Outer for loop.
  for(int i = 1; i <= 5; i++)
  {
 // Inner for loop.
    for(int j = 1; j <= i; j++){
      System.out.print(j+ " ");
    }
   System.out.println(" ");
  }
 }
}
Output:
      Displaying right triangle pattern of numbers: 
      1  
      1 2  
      1 2 3  
      1 2 3 4  
      1 2 3 4 5 

Program code 5:

public class PatternEx {
public static void main(String[] args) 
{
 int k = 1;	
System.out.println("Displaying right triangle pattern of numbers: ");	
// Outer for loop.
  for(int i = 1; i <= 5; i++)
  {
 // Inner for loop.
	for(int j = 1; j <= i; j++){
     System.out.print(k+ " ");
    }
   System.out.println(" ");
   k++;
  }
 }
}
Output:
     Displaying right triangle pattern of numbers: 
     1  
     2 2  
     3 3 3  
     4 4 4 4  
     5 5 5 5 5  

3. Let’s create a Java program to the display the following pattern using nested for loops.

Program code 6:

public class PatternEx {
public static void main(String[] args) 
{
 System.out.println("Displaying pattern of numbers: ");	
// Outer for loop.
  for(int i = 5; i >= 1; i--)
  {
 // Inner for loop.
   for(int j = 1; j <= i; j++) {
     System.out.print(j+ " ");
    }
   System.out.println(" ");
  }
 }
}
Output:
     Displaying pattern of numbers: 
     1 2 3 4 5  
     1 2 3 4  
     1 2 3  
     1 2  
     1  

Alphabet Pattern using Nested for loops

4. Let’s write program code to print the following alphabet pattern using Java nested for loops.

Program code 7:

public class PatternEx {
public static void main(String[] args) 
{
 System.out.println("Displaying alphabet pattern: ");	
// Outer for loop.
  for(int i = 65; i <= 69; i++)
  {
 // Inner for loop.
	for(int j = 65; j <= i; j++){
	 char ch = (char)j;	
     System.out.print(ch+ " ");
    }
   System.out.println(" ");
  }
 }
}
Output:
     Displaying alphabet pattern: 
     A 
     A B 
     A B C 
     A B C D 
     A B C D E 

Program code 8:

public class PatternEx {
public static void main(String[] args) 
{
 System.out.println("Displaying alphabet pattern: ");	
// Outer for loop.
  for(int i = 69; i >= 65; i--)
  {
 // Inner for loop.
   for(int j = 65; j <= i; j++) {
     char ch = (char)j;	
     System.out.print(ch+ " ");
    }
   System.out.println(" ");
  }
 }
}
Output:
     Displaying alphabet pattern: 
     A B C D E  
     A B C D  
     A B C  
     A B  
     A  

More Example Patterns for Best Practice

Program code 9:

public class PatternEx {
public static void main(String[] args) 
{
 int k = 65;	
 System.out.println("Displaying alphabet pattern: ");	
// Outer for loop.
  for(int i = 65; i <= 69; i += 2) 
  { 
// Inner for loop. 
   for(int j = 69; j >= 65; j--)
   {
     if(j > i)
	System.out.print(" ");	
     else	 
        System.out.format("%c ", k++);
    }
   System.out.println(" ");
  }
 }
}
Output:
      Displaying alphabet pattern: 
      A  
    B C D  
   E F G H I  

Program code 10:

public class PatternEx {
public static void main(String[] args) 
{
 int i, j, k = 1;
 for(i = 1; i <= 5; i += 2) 
 { 
   for(j = 5; j >= 1; j--)
  {
    if(j > i)
       System.out.print(" ");
    else
       System.out.print(k++ +" ");  
   }
   System.out.println();
  }
 }
}
Output:
      1 
    2 3 4 
  5 6 7 8 9

In this tutorial, you learned nested for loop in Java with various example programs. Hope that you will have understood the basic points of nested for loop and practiced the following patterns.
Thanks for reading!!!

Next ⇒ For each loop in Java

⇐ Prev Next ⇒