What is Nested For Loop in Java?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

What is Nested for Loop in Java?

Before learning about the nested for loop in java, let us first discuss the for loop.

The for loop in java (or any other programming language) is a entry controlled loop which checks the certain condition(s) before repeatedly executing the statement present inside the for loop. In simple terms, we can say that the for loop executes a certain block of code a specified number of times.

The syntax of for loop in java is quite simple:

The for loop in Java consists of three things.

  1. Initialization: It contains the initialization of the variable which helps to keep track of the number of iterations.
  2. Condition: It contains the condition that needs to be satisfied for the execution of the for loop body.
  3. Update (Increment/Decrement): It is a counter that increments or decrements the iteration count.

Refer to the flow chart (diagram below) to get a better understanding of the working of for loop.

working of for loop

Let us take a simple example to understand the working using the flow diagram.

Output:

First, the initialization of the variable i is done, which will help us track the number of iterations. The second part in the for loop (i.e., i < 5) is called the condition. When the condition is satisfied (or when the condition is true), the body of the for loop gets executed, and the iteration count is increased. The third part of the for loop (i.e., i = i + 1) is responsible for increasing the iteration count.

The above steps, apart from the initialization, are performed each time until the condition becomes false. Once the condition becomes false, we come out of the loop body.

Let us now learn about the topic, i.e., the nested for loop in Java. When there is one or more for loops inside a for loop, we call it a nested for loop in Java. Since one for loop is inside another for loop just like a nest, we call it a nested for loop in Java. Refer to the diagram below to understand and visualize the nested for loop in Java.

nested for loop in java diagram

First the outer for loop is checked and for every iteration of outer for loop the inner for loop is executed. Refer to the section - "Working of Nested for Loop in Java" for a flow diagram and detailed explanation with examples.

Note: Java programming also supports two other kinds of loops namely - the while loop and the do while loop. The while loop is also entry controlled whereas the do while loop is an exit controlled loop.

Syntax for Nested For Loop

The syntax of the nested for loop in java is quite simple.

First the outer for loop is checked and for every iteration of outer for loop inner for loop is executed. Now, let us learn the working of nested for loop in java in detail.

Working of Nested for Loop in Java

Let us now look at the flow chart of the nested for loop in java.

Working of Nested for Loop in Java

Let us take a small example to understand the working with the help of the flow chart describes above.

Example:

Working:

  1. The program execution gets started from the outer for loop. Outer for loop variable is initialized (i.e. int i = 1;) and the provided condition is checked. If the condition of the outer for loop is satisfied (the outer for loop condition is true) then, we come inside the outer for loop body.
  2. Now, the control of the execution is transferred to the inner for loop, and the inner loop variable is initialized (i.e. int j = 1;).
  3. After the initialization, the inner for loop condition is checked. Similar to the outer for loop, if the condition is true then we move inside the inner for loop body.
  4. The body of the inner for loop is executed and the counter is increased or decreased as per the code.
  5. Again the condition is checked and like this, the inner for loop body will be executed 2 times.
  6. When the execution of the inner for loop is completed, the control goes back to the outer for loop and the value of i is increased because the body of the outer for loop has been executed once.

In this way, the outer for loop is executed a total of 4 times. So, the output of the above code is:

We can check out that for each value of i (from 1 to 4), the value is 1 and 2.

Examples Nested Loops in Java

Let us take a few examples to understand the working of the nested for loop in java.

Java Nested for Loop

Let us take an example of having three for loops.

Example:

Output:

We can see in the example above for a value of i, first we get into the inner for loop with the j variable, and for each value of j variable, the innermost loop is executed.

Java for Loop Inside the While Loop

Just like the for loop, we have another loop in java called the while loop.

Syntax:

The while loop executes the set of statements inside it if the while condition is satisfied (true). Let us take an example to understand the working of both the loops better.

Example:

Output:

Java Nested Loops to Create a Pattern

Pattern printing is widely done using nested for loops. Let us learn a few pattern printing examples and their code.

1. Hollow Rectangle:

Pattern:

Code:

2. Pyramid

Pattern:

Code:

Output:

3. Reverse Pyramid

Pattern:

Code:

Output:

Conclusion

  • The for loop in java (or any other programming language) is a conditional iterative statement which checks the certain condition(s) before repeatedly executing the statement present inside the for loop.
  • The for loop in java consists of three things.
    1. initialization: It contains the initialization of the variable which helps to keep track of the number of iterations.
    2. condition: It contains the condition that needs to be satisfied for the execution of the for loop body.
    3. increment or decrement: It is a counter that increments or decrements the iteration count.
  • When there is one or more for loops inside a for loop then we call it a nested for loop in java.
  • Since one for loop is inside another for loop just like a nest we call it a nested for loop in java. The execution of the looping program gets started from the outer for loop.
  • At each iteration of the outer for loop, the inner for loop is executed and the increment or decrement counter of the outer for loop is done.
  • The syntax of the nested for loop in java is quite simple.