- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Nested Loops
In Java, a nested loop is simply a loop placed inside the body of another loop. This structure is incredibly powerful when you need to work with multi-dimensional data or perform repetitive tasks that require multiple levels of logic. Think of it like a clock: the minute hand must complete a full circle (the inner loop) before the hour hand can move forward by just one tick (the outer loop).
Syntax:
for (initialization1; condition1; update1) {
// This code runs once per outer iteration
for (initialization2; condition2; update2) {
// This code runs multiple times for every single outer iteration
}
}
for loops are the most common choice for nesting, you can nest any type of loop. You can put a while loop inside a for loop, or even a do-while inside another do-while depending on your specific logic requirements.
i) for both the outer and inner loops. This will cause unexpected behavior or infinite loops because the inner loop will overwrite the value the outer loop is trying to track. Always use distinct names like i and j.
Nested Loop Example:
- In this example, the outer loop tracks rows, and the inner loop tracks columns. For every increment of
i, thejloop runs its full course from 1 to 3.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println("i = " + i + ", j = " + j);
}
}
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
Use Cases:
- Processing 2D Arrays: If you're building a game board (like Chess or Tic-Tac-Toe), you use nested loops to iterate through the rows and columns of a grid.
- Generating Tables: Creating multiplication tables or formatting text-based reports into rows and columns.
- Pattern Matching: Comparing every element in a list against every other element in the same list (common in basic sorting algorithms like Bubble Sort).
- Coordinate Systems: Mapping out X and Y coordinates for graphics or UI layouts.
i and j become confusing. If you are iterating over a grid, use row and col. This makes your code much easier for other developers to read and maintain.
outerLoop: for(...)) and then call break outerLoop; from inside the inner loop.
Summary
Nested loops provide a way to perform repetitive tasks with multiple levels of iteration in Java. By understanding the relationship between the outer and inner loops, you can handle complex data structures like matrices and implement essential algorithms. Just remember to keep an eye on performance and variable naming to ensure your code remains clean and efficient.