Syntax
Basic Structure of Nested For Loops
for (initialization; condition; update) {
for (initialization; condition; update) {
// Inner loop code
}
// Outer loop code
}
Basic Structure of Nested While Loops
while (condition) {
while (condition) {
// Inner loop code
}
// Outer loop code
}
Basic Structure of Nested Do-While Loops
do {
do {
// Inner loop code
} while (condition);
// Outer loop code
} while (condition);
Examples of Nested Loops in Java
Example 1: Nested For Loop
Java
public class NestedForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println("i: " + i + ", j: " + j);
}
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 2, j: 1
i: 2, j: 2
i: 2, j: 3
i: 3, j: 1
i: 3, j: 2
i: 3, j: 3
Explanation:
- The outer loop runs 3 times.
- For each iteration of the outer loop, the inner loop runs 3 times.
Example 2: Nested While Loop
Java
public class NestedWhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 3) {
int j = 1;
while (j <= 3) {
System.out.println("i: " + i + ", j: " + j);
j++;
}
i++;
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 2, j: 1
i: 2, j: 2
i: 2, j: 3
i: 3, j: 1
i: 3, j: 2
i: 3, j: 3
Explanation
- The outer loop uses a while loop to run 3 times.
- The inner loop also uses a while loop to run 3 times for each iteration of the outer loop.
Example 3: Nested Do-While Loop
Java
public class NestedDoWhileLoopExample {
public static void main(String[] args) {
int i = 1;
do {
int j = 1;
do {
System.out.println("i: " + i + ", j: " + j);
j++;
} while (j <= 3);
i++;
} while (i <= 3);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 2, j: 1
i: 2, j: 2
i: 2, j: 3
i: 3, j: 1
i: 3, j: 2
i: 3, j: 3
Explanation
- The outer loop uses a do-while loop to run 3 times.
- The inner loop also uses a do-while loop to run 3 times for each iteration of the outer loop.
Common Use Cases of Nested Loops
Printing Patterns
Nested loops are often used to print patterns. For example, a simple star pattern can be printed using nested loops.
Java
public class StarPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
*
**
***
****
*****
Iterating Over Multi-Dimensional Arrays
Nested loops are ideal for iterating over multi-dimensional arrays.
Java
public class MultiDimensionalArray {
public static void main(String[] args) {
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
1 2 3
4 5 6
7 8 9
Frequently Asked Questions
What are nested loops in Java?
Nested loops are loops inside another loop, used for iterating over multi-dimensional data or creating patterns.
When should nested loops be used?
Use nested loops when you need to perform repeated actions on multi-dimensional data or when implementing complex algorithms.
How to optimize nested loops?
Optimize by minimizing iterations, breaking down loops into methods, and ensuring loops are necessary.
Conclusion
Nested loops in Java are powerful for handling complex data structures and algorithms. Understanding their syntax, use cases, and best practices ensures you can leverage them effectively in your programs. By following performance considerations and avoiding common pitfalls, you can write efficient and readable code using nested loops.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.