A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or for. For example, the inner loop can be a while loop while an the outer loop can be a for loop. Of course, they can be the same kind of loops, too.
Both, Java and Python allow to use one loop inside another loop. Following section shows a few examples to illustrate the concept.
Nested
...
while loops
The following program uses a nested for loop to find the prime numbers from 2 to 100:
|
|
Nested for loops
You can often use for-loops to make code easier to read. The following code with nested for-loops prints...
...