DEV Community

Cover image for πŸš€ Day 28 of My Automation Journey – Nested For Loop
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 28 of My Automation Journey – Nested For Loop

Today’s learning was super important πŸ‘‡

πŸ‘‰ Moving from single loops β†’ nested loops
πŸ‘‰ This is where pattern logic begins πŸ”₯


πŸ”Ή 1. What is a Nested Loop?

πŸ‘‰ A loop inside another loop is called a nested loop

πŸ’‘ Structure:

for(row) {
    for(column) {
        // logic
    }
}
Enter fullscreen mode Exit fullscreen mode

🧠 How It Works

πŸ‘‰ Outer loop β†’ controls rows
πŸ‘‰ Inner loop β†’ controls columns


πŸ”Ή 2. Basic Example (Print 1’s)

for(int row = 1; row <= 2; row++) {
    for(int col = 1; col <= 5; col++) {
        System.out.print(1);
    }
    System.out.println();
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output:

11111
11111
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 3. Print Row Number

for(int row = 1; row <= 2; row++) {
    for(int col = 1; col <= 5; col++) {
        System.out.print(row);
    }
    System.out.println();
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output:

11111
22222
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 4. Print Column Number

for(int row = 1; row <= 2; row++) {
    for(int col = 1; col <= 5; col++) {
        System.out.print(col);
    }
    System.out.println();
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output:

12345
12345
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 5. Rectangle Pattern ⭐

for(int row = 1; row <= 3; row++) {
    for(int col = 1; col <= 5; col++) {
        System.out.print("*");
    }
    System.out.println();
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output:

*****
*****
*****
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 6. Reverse Triangle Pattern πŸ”»

int count = 5;

for(int row = 1; row <= 5; row++) {
    for(int col = 1; col <= count; col++) {
        System.out.print("*");
    }
    System.out.println();
    count--;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output:

*****
****
***
**
*
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 7. Triangle Pattern πŸ”Ί

int count = 1;

for(int row = 1; row <= 5; row++) {
    for(int col = 1; col <= count; col++) {
        System.out.print("*");
    }
    System.out.println();
    count++;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output:

*
**
***
****
*****
Enter fullscreen mode Exit fullscreen mode

πŸ” Key Concept (Very Important πŸ”₯)

πŸ‘‰ Inner loop runs completely for each outer loop iteration

Example:

  • Row 1 β†’ full columns execute
  • Row 2 β†’ full columns execute again

⚠️ Common Mistake

πŸ‘‰ Forgetting System.out.println();

System.out.print("*"); // stays in same line
System.out.println();  // moves to next line
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Real Understanding Trick πŸ’‘

Think like a table:

Row Columns Execution
1 1 β†’ 5
2 1 β†’ 5

πŸ‘‰ That’s how patterns are built


πŸ”š Final Takeaways

βœ” Nested loop = loop inside loop
βœ” Outer loop β†’ rows
βœ” Inner loop β†’ columns
βœ” Used for pattern problems
βœ” Base for advanced logic


πŸ’‘ Golden Insight

πŸ‘‰ If you understand nested loops, you unlock pattern programming πŸ”“


πŸ€– A Small Note
I used ChatGPT to help structure and refine this blog while keeping the concepts aligned with my learning.

Top comments (0)