Nested For Loop in Java
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 program 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 an 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 the 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.

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;
}
}
Nested For Loops Example Programs
Let’s take an example program in which we will display values of the inner for loop for each outer iteration, as well as outer for loop.
Example 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 a total of four times.
Let’s take an example in which we will display tables for 1 and 2 using nested for loops in Java.
Example 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
Java Pattern Examples using Nested For Loop
Let’s take an example program in which we will display a triangle of * using nested for loop.
Example 3:
public class PatternEx {
public static void main(String[] args)
{
System.out.println("Displaying a 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 a right triangle of *: * * * * * * * * * * * * * * *
Let’s take an example program to display a triangle of numbers in Java.
Example 4:
public class PatternEx {
public static void main(String[] args)
{
System.out.println("Displaying a 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 a right triangle pattern of numbers: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Example 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 a right triangle pattern of numbers: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Let’s consider an example program to the display the following pattern using Java nested for loops.
Example 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
Let us take example programs to print the following alphabet pattern in Java using nested for loops.
Example 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
Example 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 Practices
Example 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
Example 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
Advantages of Nested For Loops
There are the following advantages of using nested for loops in Java. They are as:
- Nested for loops are suitable for handling multidimensional arrays and matrices, simplifying data processing.
- They help us to solve complex tasks in which we need multiple levels of iteration.
- We commonly used them for generating various patterns and shapes in the output.
Limitations of Nested For Loops
Although nested for loops are powerful, but they also have some limitations:
- Excessive nesting can impact the performance of code as the number of iterations increases significantly.
- Excessive nesting can make code harder to read, understand, and maintain.
Tips for Using Nested For Loops Efficiently
To use of nested for loop efficiently in Java, you should keep the following points in mind.
- Keep it simple.
- Avoid excessive levels of nesting. Stick to two levels whenever possible.
- Optimize loop conditions to minimize unnecessary iterations.
- Keep proper indentation to enhance code readability.
- Add proper comments to explain the aim of nested loops, especially when working with complex tasks.
- Evaluate your code thoroughly and debug any issues that arise during the execution of the program.