+ 6

How the nested loop works in c++

Logic behind the nested loop

5th Mar 2019, 1:44 PM
Pallavi Bankar
Pallavi Bankar - avatar
3 odpowiedzi
+ 5
Thanks for the help👍
5th Mar 2019, 1:53 PM
Pallavi Bankar
Pallavi Bankar - avatar
+ 3
Thanks
16th Mar 2019, 7:45 PM
Sally(Codder)
Sally(Codder) - avatar
+ 2
Nested FOR loop means a for loop within a FOR loop. It does not how many loops are there in a FOR loop. A instance of this,if u consider this loop for (int i=1;i<10;i++) // loop 1 { // start of outer loop for(int j=1;j<=i;j++) //loop 2 { // start of inner loop cout << j << endl; } // end of inner loop } // end of outer loop 1. In loop 1 Control , assign i=1 then checks condition of i<10 after that control shifts to loop 2 2 Now In loop 2 , j is assign =1, then control checks for condition j<=1 i.e 1<=1 so now as condition is true,it now moves inward now it finds statement to print the value of j so it prints 1 now control moves to update statement of loop 2 where increment of j occurs and value of j becomes 2. 3 Now control shifts to condition( of loop 2 ) j<=i,but now j=2 so condition is false so control moves out of the inner loop and control is transferred to the outer loop's update part(i++) 4 As i becomes 2, there is a check of condition (i<10), after than again same as step 2 j=1, prints 1 2 when i=3 it prints 1 2 3 likewise it executes till i=10....
5th Mar 2019, 1:52 PM
Prokopios Poulimenos
Prokopios Poulimenos - avatar