Nested for loop in Java: Syntax, Flowchart, and Practical Examples

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.

Read More: If you want to read about for-loop in detail then click here

Syntax

//Outer loop
for (initialization; condition; update) {

    //Inner loop
    for (initialization; condition; update) {
        // Code to be executed
    }
}

Explanation:

  1. Syntax starts with the keyword for.
  2. The first loop is the outer loop.
  3. The second loop inside the outer loop is the inner loop.
  4. initialization is used to initialize the loop variable followed by a semicolon (;) in both loops.
  5. condition is used to check the test condition followed by a semicolon (;) in both loops.
  6. update is used for increment and decrement operations in both loops.
  7. Three expressions are separated by a semicolon(;) in both for loop headers.
  8. In other words, we can say that the inner loop is the body of the outer loop.
Note : In Java, a local variable declared in the inner for loop is not accessible outside that inner loop. In other words, We cannot access inner loop variable in the outer loop. But, Variables declared in the outer loop can be accessed inside the inner loop.

Flowchart

Nested For loop Flowchart in java

Let’s understand the working of nested for loop with the help of a flowchart.

Explanation:

  1. Control starts from the top and moves to the Outer variable initialization and initializes the outer loop variable.
  2. After initialization, the control moves to the Outer Condition and checks the test condition.
  3. If the Outer Condition is true then the control moves to theInner varibale initialization and initializes the inner loop variable.
  4. After initialization of the inner loop variable control moves to the Inner Condition and checks the inner test condition.
  5. 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.
  6. 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.
  7. 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.
  8. After updating the value of the outer loop variable control moves to the Outer Condition and tests the outer condition.
  9. 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.
  10. 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:

  1. Class name is HalfPyramid.
  2. Program execution starts from the main() method.
  3. Initialize the variable rows and assign the value 5.
  4. Control moves to the outer loop and initializes the variable i=1; and then checks the condition i<=rows, now the value of i is 1 and the value of rows is 5 and the condition is (1<=5) means true.
  5. 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.
  6. 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.
  7. For the first time, the inner loop prints the one-star (*).
  8. After the inner loop, System.out.println() will be executed for entering a new line.
  9. Then control moves to the outer loop increment part, increments the value then checks the outer loop test condition.
  10. If the outer loop test condition is true then the control moves to the inner loop and repeats the process.
  11. For the second time, the inner loop prints the two stars (**).
  12. For the third time, the inner loop prints the two stars (***).
  13. For the fourth time, the inner loop prints the two stars (****).
  14. For the fifth time, the inner loop prints the two stars (*****).
  15. 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:

For loop In Java

While Loop In Java

do while loop in Java

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Aitechray