Nested for loop in Java means one for loop is present inside another for loop. It is similar to the nested-if statement where one if statement is present inside another if statement.
In the case of nested for loop, the internal loop is called Inner loop
, and the external loop is called outer loop
.
The inner loop runs completely on each iteration of the outer loop. We can write N numbers of the for loop inside the other for-loop.
Nested for loop is mostly used when we work with multi-dimensional arrays.
Syntax
//Outer loop
for (initialization; condition; update) {
//Inner loop
for (initialization; condition; update) {
// Code to be executed
}
}
Explanation:
- Syntax starts with the keyword
for
. - The first loop is the outer loop.
- The second loop inside the outer loop is the inner loop.
initialization
is used to initialize the loop variable followed by a semicolon (;) in both loops.condition
is used to check the test condition followed by a semicolon (;) in both loops.update
is used for increment and decrement operations in both loops.- Three expressions are separated by a semicolon(;) in both for loop headers.
- In other words, we can say that the inner loop is the body of the outer loop.
Flowchart
Let’s understand the working of nested for loop with the help of a flowchart.
Explanation:
- Control starts from the top and moves to the
Outer variable initialization
and initializes the outer loop variable. - After initialization, the control moves to the
Outer Condition
and checks the test condition. - If the Outer Condition is true then the control moves to the
Inner varibale initialization
and initializes the inner loop variable. - After initialization of the inner loop variable control moves to the Inner Condition and checks the inner test condition.
- If the Inner Condition is true then the body of the inner loop will be executed and after the execution is complete control moves to the
Inner-Increment/Decrement
and updates the value of inner loop variable. - After increment or decrement control moves to the Inner Condition and tests the inner condition again and repeats the previous process until the Inner Condition becomes false.
- If the Inner Condition is false then the Inner Body will not execute and the control moves to the Outer Increment/Decrement and updates the value of the outer loop variable.
- After updating the value of the outer loop variable control moves to the Outer Condition and tests the outer condition.
- If the outer test condition is true then the control moves to the inner loop and repeats the previous process again until the inner loop condition becomes false.
- If the Outer Condition is false then both loop bodies will be skipped and the control will move to the Next Statement in the program.
Nested for loop example in Java- Pattern Program
public class HalfPyramid {
public static void main(String[] args) {
int rows = 5; // Number of rows in the half pyramid
// Outer loop for each row
for (int i = 1; i <= rows; i++) {
// Inner loop for printing stars
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
// Move to the next line after each row
System.out.println();
}
}
}
Explanation:
- Class name is
HalfPyramid.
- Program execution starts from the
main()
method. - Initialize the variable
rows
and assign the value5.
- Control moves to the outer loop and initializes the variable
i=1;
and then checks the conditioni<=rows
, now the value of i is 1 and the value of rows is 5 and the condition is(1<=5)
means true. - The Outer test condition is true so the control moves to the inner loop and initializes the variable
j=1;
and checks the inner loop condition(j<=i)
, now j value is 1 and i value is 1, and the condition(1<=1)
is true and body of the inner loop will be executed. - After the inner loop body execution control moves to the increment part and updates the value of j from 1 to 2 (incrementing), again inner test condition is checked, and if it is true then the inner loop body is executed again.
- For the first time, the inner loop prints the one-star
(*).
- After the inner loop,
System.out.println()
will be executed for entering a new line. - Then control moves to the outer loop increment part, increments the value then checks the outer loop test condition.
- If the outer loop test condition is true then the control moves to the inner loop and repeats the process.
- For the second time, the inner loop prints the two stars
(**).
- For the third time, the inner loop prints the two stars
(***).
- For the fourth time, the inner loop prints the two stars
(****).
- For the fifth time, the inner loop prints the two stars
(*****).
- For the sixth time, the outer loop test condition becomes false, and then the loop body will not execute.
Conclusion
The nested for loop in Java is essential for working with multidimensional data structures like matrices or performing complex iterations within iterations. By understanding how to manage nested loops effectively, you can solve a wide range of programming challenges. Keep practicing with varied examples to solidify your skills and explore advanced uses of nested loops in real-world applications.
We’d Love to Hear From You!
Do you have any questions or feedback about nested for loops in Java? We’d love to assist you! Whether you’re seeking clarification, have suggestions, or want to share your thoughts, feel free to leave a comment below. Your input helps us improve our content and benefits other learners. We’re excited to hear from you!
Related Articles: