The general syntax to nest while loop inside a for loop is as: for(initialization test-condition increment/decrement) Thus, we can also nest a while loop or a do-while loop inside a for loop and vice versa. The basic flowchart diagram for nested loops in Java has shown in the below figure. Flowchart Diagram for Nested Loops in Java In the same way, we can also nest a while loop or a do-while loop inside a for loop and vice versa. Since the inner for loop will execute 4 times for each i value therefore, statements2 inside the loop body will execute 12 times. Hence, statement1 inside the loop body will also execute 3 times. In the above sequence, the outer for loop will execute total 3 times. This means that the values of i and j will change as: Again, the inner for loop will execute 4 times. This time, the control of execution again enters inside the inner for loop and statement2 will execute 4 times.į) Then, the control again goes to outer for loop and the value of i will set to 3. Now, the value of i will be 2 because of increment by 1. This means that statement2 will execute 4 times in the first execution of outer for loop.Į) When the execution of inner for loop completes, the control of execution goes to the outer for loop. Then, statement2 will execute once again.ĭ) Like this, the inner for loop will execute 4 times with changing j values from 1 to 4. Since we initially set the control variable j to 1, statement2 will execute once.Ĭ) After this execution, the value of j will be 2 because of increment by 1. B) Now, the control of execution enters inside the inner for loop.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |