Core Java Fundamentals ยท Loops

Nested Loops

Learn how to use loops inside other loops to handle more complex repetitive tasks, like working with two-dimensional data or creating patterns.

NestedLoopDemo.java

public class NestedLoopDemo {
    public static void main(String[] args) {
        int rows = 3;
        int cols = 4;

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            // Inner loop for columns
            for (int j = 1; j <= cols; j++) {
                System.out.print("* ");
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Step by step

  1. The program starts execution inside the main method. We declare an integer variable rows and initialize it to 3.
  2. Next, we declare another integer variable cols and initialize it to 4.
  3. The outer loop begins. A new variable i is initialized to 1. This variable will control the rows.
  4. The condition i <= rows (1 <= 3) is checked. It is true, so the body of the outer loop will execute.
  5. Now, the inner loop begins. A new variable j is initialized to 1. This variable will control the columns for the current row.
  6. The inner loop condition j <= cols (1 <= 4) is checked. It is true, so the body of the inner loop executes.
  7. System.out.print("* ") is executed. It prints an asterisk followed by a space. The string "* " is added to the string pool. The cursor remains on the same line.
  8. The update statement j++ is executed. j becomes 2. The control goes back to the inner loop condition.
  9. The condition j <= cols (2 <= 4) is checked again. It is true, so the inner loop body executes again.
  10. Another "* " is printed on the same line.
  11. j++ is executed. j becomes 3.
  12. The condition j <= cols (3 <= 4) is true.
  13. Another "* " is printed on the same line.
  14. j++ is executed. j becomes 4.
  15. The condition j <= cols (4 <= 4) is true.
  16. The final "* " for this row is printed.
  17. j++ is executed. j becomes 5.
  18. The condition j <= cols (5 <= 4) is now false. The inner loop terminates. The variable j goes out of scope.
  19. System.out.println() is executed, which moves the cursor to the next line in the console.
  20. The first iteration of the outer loop is complete. The update statement i++ is executed. i becomes 2.
  21. The condition i <= rows (2 <= 3) is checked. It is true, so the outer loop body executes for the second time.
  22. The inner loop is re-initialized. j is set to 1 again.
  23. The condition j <= cols (1 <= 4) is true, so the inner loop starts for the second row.
  24. System.out.print("* ") is executed for the second row.
  25. This inner loop will execute 4 times again, just like the first time. j will go from 1 to 4, printing "* " each time.
  26. After j becomes 5, the condition 5 <= 4 is false, and the inner loop terminates.
  27. A new line is printed, completing the second row.
  28. The second iteration of the outer loop is complete. i++ is executed, and i becomes 3.
  29. The condition i <= rows (3 <= 3) is checked. It is true, so the outer loop executes for the third time.
  30. The inner loop runs again for the third row, printing "* " four times.
  31. A new line is printed, completing the third and final row.
  32. The third iteration of the outer loop is complete. i++ is executed, and i becomes 4.
  33. The condition i <= rows (4 <= 3) is checked. It is now false, so the outer loop terminates.
  34. The main method has finished executing. The program ends.