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.
Simple Nested Loop
Pattern Nested Loop
Logical Nested Loop
In this blog, we will discuss on Pattern Nested Loops in java program.
Some Images on Pattern Nested Loop Program.
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 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.
- First Example is 11111 in left side
- Second Example is 11111 in left side
- Third Example is 11111 in right side
- Eight Example 55555 in right side
- Now see the opposite side of common side.
- 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++)
- 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–)
- 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.
- If number is 1, then set for(j=1;j<=i;i++).
- 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.
After getting all output, you have to recheck your program and output. And Fallow the step by step nested loop.
Share and Enjoy
286total visits,1visits today