![]() |
VOOZH | about |
A nested loop means a loop statement inside another loop statement.
Nested for loop refers to any type of loop that is defined inside a 'for' loop.
i = 0: 0 1 2 3 4 i = 1: 5 6 7 8 9 i = 2: 10 11 12 13 14 i = 3: 15 16 17 18 19
Below is the equivalent flow diagram for nested 'for' loops:
👁 Nested-for-LoopA nested while loop refers to any type of loop that is defined inside a 'while' loop.
Pattern Printing using Nested While loop 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Below is the equivalent flow diagram for nested 'while' loops:
👁 Nested-while-LoopA nested do-while loop refers to any type of loop that is defined inside a do-while loop.
1 2 3 1 2 3
Below is the equivalent flow diagram for nested 'do-while' loops:
👁 Nested-do-while-LoopA hybrid loop structure is where any type of loop is nested inside another loop type.
i = 1, j = 1 i = 1, j = 2 i = 2, j = 1 i = 2, j = 2
Whenever we use a break statement inside the nested loops it breaks the innermost loop only and program control goes to the outer loop. Breaking the inner loop does not affects the outer loops.
* * * * * * * * * * * *
In the above program, the first loop will iterate from 0 to 5 but here if i will be equal to 3 it will break and will not print the * as shown in the output.
Whenever we use a continue statement inside the nested loops it skips the iteration of the innermost loop only. The outer loop remains unaffected.
0 1 0 1
In the above program, the inner loop will be skipped when j will be equal to 2. The outer loop will remain unaffected.