Nested For Loop in Java

Placing For Loop inside another For Loop is called as Nested For Loop in Java Programming. When you are working with multi-layered data, then use this Java Nested For loops to extract the layered data, but please be careful while using it.

For example, when you are working with single-dimensional Array, you can use Java For Loop to iterate from starting to array end. However, to work with Two Dimensional array or Multi Dimensional Array, you have to use this Nested For Loop in Java. Before we get into the example, let us see the syntax of Java Nested For loop.

Nested For loop in Java Syntax

The syntax of the Nested For Loop in Java Programming language is as follows:

for (initialization; test condition; increment/decrement operator)  { //First For Loop
     for (initialization; test condition; increment/decrement operator)  { // Second For Loop
         //Second For Loop Statements
         Statement 1
         Statement 2
         ………
         Statement n
     }
     //First For Loop Statements
     Statement 1 
      ………
     Statement n
}

If you observe the above Java Nested For Loop syntax, We placed the For loop inside another for loop.

We have already explained the for loop syntax in our previous article. So, please refer to Java For Loop article to understand the loop functionality. Let me explain the details of this Java Nested For Loop syntax

Step 1: First, the compiler will check for the condition inside the first for loop.

  • If the condition is True, then statements inside the For loop will be executed. It means the compiler will enter into second For loop: Goto, Step 2.
  • If the condition is False, then the compiler will exit from For Loop.

Step 2: Java compiler will check for the condition inside the second for loop or nested for loop.

  • If the condition is True, statements inside the second For loop will execute. It means, it will execute from Statement 1 to N.
  • If the condition is False, the compiler will exit from second For Loop.

Step 3: Once it exits from second for loop, the compiler will check for the condition inside the for loop (repeating Step 1 ).

Nested For Loop in Java Programming

This Nested for loop Java program allows the user to enter any integer values. Then it will print the Multiplication table from the user-specified number to 10. To do this, we are going to nest one for loop inside another for loop. This also called nested for loop in java programming.

// Nested For Loop in Java Programming example
package Loops;

import java.util.Scanner;

public class NestedForLoop {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int i, j;
		sc = new Scanner(System.in);	
		
		System.out.println("Please Enter the any integer Value below 10: ");
		i = sc.nextInt();
		
		for (; i <= 10; i++)  {
			for (j = 1; j <= 10; j++)  {
				System.out.format("%d * %d = %d\n",i ,j, i*j);
			}
			System.out.println();
		}
	}
}
Nested For Loop in Java 1

In this Java Nested For Loop example, the following statements ask you to enter any integer value below 10. Next, we are assigning the user entered value to integer variable (i)

System.out.println("Please Enter the any integer Value below 10: ");
i = sc.nextInt();

Next line, we used the Java Nested For loop, and the Condition inside the First For loop will make sure that the user-specified value i is less than or equal to 10.

for (; i <= 10; i++)  {
	for (j = 1; j <= 10; j++)  {
		System.out.format("%d * %d = %d\n",i ,j, i*j);
	}
	System.out.println();
}

From the above screenshot, you can observe that the User Entered value i: = 9. It means, This Java Nested For Loop program prints the multiplication table for 9 and 10.

First For Loop First Iteration

In the first for loop, i initialized to value 9, and then it will check whether i is less than or equal to 10. This condition is True so, it will enter into second for loop.

Second For Loop First Iteration

It is the Nested For loop in Java. In the second for loop j is initialized to value 1. Next, it will check whether j is less than or equal to 10. This condition is True so, the Javac compiler will execute the statements inside the second for loop.

i * j ==> 9 * 1 = 9

Next, j value will increment by 1 (j++). Please refer to Increment and Decrement Operators in Java article to understand this ++ notation.

Second For Loop Second Iteration

Here, j is incremented by 1. So, J =2. Compiler will check whether j is less than or equal to 10. This condition is True so that the compiler will execute the statements inside the second for loop.

i * j ==> 9 * 2 = 18

Next, j value will increment by 1 (j++).

This process will repeat until j reaches to 11. Once the condition inside the second for loop fails, the compiler will exit from second for loop, and i will increment by 1 (i++). Next,

First For Loop Second Iteration

Here, i is incremented by 1 so, i =10. Compiler will check whether i is less than or equal to 10. This condition is True so, compiler will enter into second for loop

Second For Loop First Iteration

In the second for loop, j initialized to value 1, and then it will check whether j is less than or equal to 10. This condition is True. So, the compiler will execute the statements inside the second for loop.

i * j ==> 10 * 1 = 10

Next, j value will increment by 1 (j++).

Second For Loop Second Iteration – nested for loop in java

Here, j is incremented by 1, so J =2. Compiler will check whether j is less than or equal to 10. This condition is True so that the compiler will execute the statements inside the second for loop.

i * j ==> 10 * 2 = 20

Next, j value will incremented by 1 (j++).

This process will repeat until j reaches to 11. Once the condition inside the second for loop fails, the compiler will exit from second for loop, and i will increment by 1 (i++). Next,

First For Loop Third Iteration:

Here, i = 11, and the condition is False. For loop is terminated, No need to check the second loop, i.e., nested for loop in java.

Comments are closed.