Tutorials

Java Loop Control


Java nested for loop


For loop that we worked on in above tutorial was basic one. We can use it to solve more complex problems in easier way by embedding for loop within for loop. 
 
Loop within a loop is termed as nested loop. And the for loop within the for loop is called as nested for loop.
 
Example: Nested for loop can be used to print multiplication table. We will show one application of it. Reader can apply the logic to solve various other problems.
 
public class printNumbers {

   public static void main(String[] args) {
      System.out.println("Printing multiplication table start");

      for(int i = 1; i<3; i++){                //outer loop
         for(int j=0; j<=10; j++){          //inner loop
           System.out.println(i+ "*"+ j+ "="+ i*j);
         }
          System.out.println("Multiplication table of "+ i+ " completed");
     }

   }
}
OUTPUT:
Printing multiplication table start
1*0=0
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1*10=10
Multiplication table of 1 completed
2*0=0
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
Multiplication table of 2 completed
 

How the above code works?

At first iteration, i = 1 & 1<3 is true. So control passes inside the outer for loop. 
 
Here it encounters another for loop (inner for loop). j=0 & 0<=10 is true. So control passes inside inner for loop.
 
Here it encounters print statement, System.out.println(i+ "*"+ j+ "="+ i*j). Just place the value that i & j holds as shown above. 
 
We have, i=1 & j=0 so i*j=1*0=0. Thus we get, 1*0=0
 
Notice that inner loop’s iteration is not yet complete. Thus state of outer for loop will remain unchanged until inner loop completes its turn and again passes control back to outer loop.
 
So value of i=1 will not get incremented till inner loop ends and value of j becomes greater than 10.
 
Until value of j becomes 11, inner for loop will keep on executing and we will keep getting output as,
 
i=1, j=1 so i*j=1*1=1. Thus we get 1*1=1
i=1, j=2 so i*j=1*2=1. Thus we get 1*2=2
.            .              .
.            .              .
i=1, j=10 so i*j=1*10=10. Thus we get 1*10=10
 
j=11 and 11<=10 evaluates to false. Thus control will pass outside of inner for loop this time. Here it encounters "Multiplication table of "+ i+ " completed"); So we get output as Multiplication table of 1 completed. And then value of i is incremented by 1 by outer for loop.
 
-------------First iteration complete-----------------------
 
Now i=2 and 2<3 is true, so control passes to inner for loop.
 
Again it follows the same procedure that we explained during first iteration.
 
i=2, j=1 so i*j=2*1=1. Thus we get 2*1=2
i=2, j=2 so i*j=2*2=1. Thus we get 2*2=4
.            .              .
.            .              .
i=2, j=10 so i*j=2*10=20. Thus we get 2*10=20
 
j=11 and 11<=10 evaluates to false. Thus control will pass outside of inner for loop this time. Here it encounters "Multiplication table of "+ i+ " completed"); So we get output as Multiplication table of 2 completed. And then value of i is incremented by 1 by outer for loop.
 
-----------------Second iteration complete---------------------
 
Now i=3 and 3<3 is false. Thus control will not enter inside outer for loop and our code execution ends.
 
You can include multiple nested for loop if your logic requires it. But when including multiple nested for loops you need to take caution   
 


Please Share this page
Views : 106
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!