Nested Loops in Java
In Java programming, a nested loop is a loop defined within a loop. A nested loop is used when a Java developer needs to perform a task that repeats multiple times on a multi-dimensional array element or an object element in a program structure. For example, when using an array matrix, a table grid, or a user-declared 2-dimensional array data element structure, a nested loop needs to be accessed or processed by a defined row column element. A nested loop can be nested within a for, while, or do-while loop in a Java program.

Nested Loop Main Element.
- Outer nested loop – An outer nested loop is a loop that surrounds an inner loop.
- Inner nested loop – An inner loop is a loop defined within an outer loop, where iteration and execution occur each time the program runs within the outer loop.
In Java programming, nested loops are mostly used to process and manage multi-dimensional array data, such as 2D arrays or table matrix data structure elements.
Syntax of a Java nested loop.
The structure of a basic nested for loop in Java programming looks like this:
for (initialization; condition; update) {
// Outer user-defined loop body
for (initialization; condition; update) {
// Inner user-defined loop body
}
}
Element of a for nested loop.
- Here, in a nested for loop, the outer loop runs first, and the inner loop runs completely each time it is iterated.
- Similarly, the inner loop repeats the process every time the outer loop runs.
Printing a table in a nested for loop example.
A common use of nested for loops in Java programs is to create multiple tables and print statements.
public class Main
{
public static void main(String[] args) {
/ Here it sets the outer loop for table rows 2 to 12
for (int p = 2; p <= 12; p++) {
/ Here it runs the inner loop for table columns 2 to 12
for (int q = 2; q <= 12; q++) {
/ Here it prints the multiply product of p and q
System.out.print(p * q + “\t”);
}
System.out.println(); // Here this statement is used to move to the next line after each table row
}
}
}
Explanation of nested for loops.
- Here the outer nested loop runs 12 times. For example, (from p = 2 to p <= 12).
- Here in the program, for every run of the outer loop, the inner loop runs 12 times. For example, (q = 2 to q = 12), and prints the multiplication of p and q for each table column.
- The \t statement indicates that the table element should be displayed in the table output pane with a clear and tab. After each repetition of the inner loop, System.out.println() moves the program cursor to the next line, starting a new table row.
Example of printing a grid of numbers in a Java nested while loop.
Java users can also use a while loop to create a table data element structure, similar to a for loop.
public class Main
{
public static void main(String[] args) {
int p = 2;
// Here we define an outer while loop for table rows
while (p <= 7) {
int q = 1;
// Here we define the inner while loop for table columns
while (q <= 7) {
System.out.print(p * q + “\t”); // Here it prints the multiplication of table rows and columns p and q
q++;
}
System.out.println(); // Here it moves to the next line after each table row
p++;
}
}
}
Explanation of a Java nested while loop.
- Here in the nested while loop, the outer while loop controls the table row (p), which is iterated 7 times (from 1 to 7) in the while loop.
- Similarly, the inner while loop controls and manages the table column (q), which is iterated 7 times (from 1 to 7), and prints the multiply statement of the variable values p and q for each loop iteration.
Example of printing a right triangle in a nested do-while loop.
A nested do-while loop can be used to print the star triangle pattern in a Java program.
public class Main
{
public static void main(String[] args) {
int p = 1;
// Here the outer do-while loop runs the table rows
do {
int q = 1;
// Here the inner do-while loop runs the table columns
do {
System.out.print(“* “); // Here it prints the star symbol in a triangle shape
q++;
} while (q <= p); // Here it repeats as many times as the current row number repeats in the loop
System.out.println(); // Here it moves to the next line after each table row ends
p++;
} while (p <= 9); // Here it repeats the star for 9 table rows
}
}
Explanation of a nested do-while loop.
- Here, the outer do-while loop manages the table row (p), which is executed 9 times in the loop. The inner do-while loop prints a star (*) statement for each table row, and the star symbol number increases with each new table row.
- For example, the star value is 1 for the first row, 2 for the second row, and so on until the loop terminates.
Use cases of nested loops in Java programming.
Nested loop matrix operations.
Nested loops in Java are used to perform multiple operations on tabular matrix or 2D array data elements. Tasks such as adding array elements, multiplying array elements, transposing array elements, etc.
Example of accessing each element of a 2D array using a nested loop in Java.
public class Main
{
public static void main(String[] args) {
int[][] matrix = {{7, 1, 8}, {2, 3, 9}, {5, 6, 4}};
for (int p = 0; p < matrix.length; p++) {
for (int q = 0; q < matrix[p].length; q++) {
System.out.print(matrix[p][q] + “
“);
}
System.out.println();
}
}
}
Creating patterns from nested loops.
In Java programming, nested loops are mostly used to create complex patterns, such as triangles, squares, pyramids, etc.
Nested loops in simulations.
Nested loops in Java programming can model or analyse real-time events involving two or more user-defined variables. For example, in simulations of grids or environments, games, or scientific modelling, etc.
Time Complexity.
The time complexity of nested loops in Java programs is a multiple of the size of the loops. For example, for (int p = 0; p < s; p++) { for (int q = r; q < t; q++) { … } }, the total time complexity of the loop is r(s * t).
Nested Loop Optimization.
Nested loops in Java programs can create performance problems, especially when working with large-volume databases. The nested program used here is important for improving the efficiency of the program source code and optimizing nested loops, such as reducing the number of iterations used in the program and using more efficient algorithms to perform them.
A Summary of Java Nested Loops.
- Nested loops in Java programming are loops defined within a program loop, and are useful when working with multi-dimensional data, such as tables, matrices, or grids.
- Nested loops are often used to create patterns and perform matrix and simulation operations.
- In a nested loop, the outer loop runs first, and for every repetition of the outer loop, the inner loop runs.
- The time complexity of nested loops is typically a multiple of the size of the loops.
