Welcome to The Coding College! In this tutorial, we’ll explore nested loops in Java. A nested loop is a loop inside another loop. It is a fundamental concept used for handling multi-dimensional data and creating complex patterns.
What Are Nested Loops?
Nested loops are used when you need to execute one loop inside another. The inner loop executes completely every time the outer loop runs one iteration.
Syntax of Nested Loops
for (initialization; condition; update) {
for (initialization; condition; update) {
// Code to execute inside the inner loop
}
}
Example 1: Printing a 2D Matrix
Code
public class NestedLoopsExample {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print("(" + i + "," + j + ") ");
}
System.out.println(); // Move to the next line
}
}
}
Output
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
Example 2: Generating Patterns
Nested loops are often used to generate patterns.
Code: Printing a Star Triangle
public class StarPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) { // Outer loop controls rows
for (int j = 1; j <= i; j++) { // Inner loop controls columns
System.out.print("* ");
}
System.out.println(); // Move to the next row
}
}
}
Output
*
* *
* * *
* * * *
* * * * *
Example 3: Multiplication Table
Code
public class MultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print(i * j + "\t");
}
System.out.println();
}
}
}
Output
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Example 4: Looping Through a 2D Array
Nested loops are ideal for working with multi-dimensional arrays.
Code
public class TwoDArray {
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();
}
}
}
Output
1 2 3
4 5 6
7 8 9
Example 5: Printing a Reverse Pyramid
Code
public class ReversePyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output
* * * * *
* * * *
* * *
* *
*
Key Points to Remember
- Inner Loop Execution: The inner loop runs fully for each iteration of the outer loop.
- Performance: Nested loops can be computationally expensive, especially with large datasets or high iteration counts.
- Applications:
- Working with multi-dimensional arrays.
- Generating complex patterns or shapes.
- Implementing algorithms like matrix operations.
Practice Problems
- Write a program to print a hollow square pattern using nested loops.
- Create a program to calculate the transpose of a 3×3 matrix.
- Generate a diamond pattern using
*
. - Iterate over a 3D array using triple nested loops.
For more tutorials on Java and programming concepts, visit The Coding College. Dive into coding with us and enhance your skills! 🚀