Nested Loop – Pattern Examples

Before starting this blog we have totally know about Simple For, while, do while and condition program and our successful last blog.

So visit Chapter – 4 and Chapter – 5 before starting this blog.

Nested Loop is very easy thing in language.

Here, I divided nested loop in 3 parts.

  1. Simple Nested Loop

  2. Pattern Nested Loop

  3. Logical Nested Loop

In this blog, we will discuss on Pattern Nested Loops in java program.

Some Images on Pattern Nested Loop Program.

nested loop

 

 

In this image, we can see 9 programs.

This image looks like mathematical practical but these are all tricks for pattern loops.

Let’s understand the programs.

Basic Structure for Nested Loop.

for(int i=1;i<=5;i++)

{

            for(int j=1;j<=5;j++)          // Nested Loop

            {

            System.out.print(j);           // Use print() method

     }

     System.out.println();        // Use println() method

}

3 Simple Steps For Pattern Loop

1st Step: Set System.out.print() line with variable i or j.

  • Find longest line from output program. I already put arrow from left to right.
  • Check longest line: if line is 12345 or 54321 then set System.out.print(j).
  • Check longest line: if line is 11111 or 55555 then set System.out.print(i).

2nd Step: Set outside loop [i loop]

  • Find common line from right side or left side. I already set yellow background on it.
  1. First Example is 11111 in left side
  2. Second Example is 11111 in left side
  3. Third Example is 11111 in right side
  4. ………
  5. Eight Example 55555 in right side
  • Now see the opposite side of common side.
    1. First Example: 11111 is common side in left so we point right side is 12345 (Top to Bottom). This is i loop. for(i=1;i<=5;i++)
    2. Second Example: 11111 is common side in left so we point right side is 54321 (Top to Bottom). This is i loop. for(i=5;i>=1;i–)
    3. ……
    4. Eight Example: 55555 is common side in right so we point left side is 54321 (Top to Bottom). This is i loop. for(i=5;i>=1;i–).

3rd Step: Set inside loop [j loop]

  • First check common side left or right. I already set yellow background.
  • If your left side is common the check what is number 1 or 5.
    1. If number is 1, then set for(j=1;j<=i;i++).
    2. If number is 5, then set for(j=5;j>=i;j–)
  • If your right side is common the check what is number 1 or 5.

    If number is 1, then set for(j=i;j>=1;j–).

    If number is 5, then set for(j=i;j<=5;j++).

 

So, do not worry about nested loops program. Just focus on these 3 simple steps and do all nested loops programs.

Note:

After getting all output, you have to recheck your program and output. And Fallow the step by step nested loop.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

312total visits,4visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

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

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint