/* */ Click to Join Live Class with Shankar sir Call 9798158723

What is Java language ?


Nested loops means that loops within loops. In other words, nested loops means, loop inside loop.

Syntex :-

for(exp1;exp2;exp3)
{
for(exp1;exp2;exp3)
{
...........
...........
}
}
The first expression : exp 1 contains initialization statements
The second expression: exp 2 contains limit–test expression
The third expression : exp 3 contains updating expression

How Nested For Loop Works in Java.

  • step 1:-
    Hear, there are two Loop first is outer loop and inside outer loop one is inner loop i.e Nested
  • step 2:-
    first the outer loop variable is initialized and then program control passes to the condition, if the condition is true, then the program control passes to the inner loop. if the condition is false then program control is terminated.
  • step 3:-
    if the condition is true, the inner loop variable will be initialized and the condition checks whether the condition is true or not if the condition is true then the inner loop statement is executed until the condition is true
  • step 4:-
    After executed inner loop now control updating the counter variable of outer loop and again the condition is checked. if the condition is true then the inner loop will be executed again.
  • step 5:-
    This process will continue until the condition of the outer loop is true


Q.) Example of Nested For Loop.
   

    public class NestedForExample {  
public static void main(String[] args) {  
//loop of i  
for(int i=1;i<=3;i++){  
//loop of j  
for(int j=1;j<=3;j++){  
        System.out.println(i+" "+j);  
}//end of i  
}//end of j  
}  
}  
Output
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3