Nested Loops

Nested Loop is located inside other loop block.

Nested for Loop

Let's have a program that prints each combination of 2 dice rolls.

Output:

Each iteration of the outer loop executes the inner loop. Basically the outer loop repeats 6 times and the inner loop 36 (6 x 6).

Nested while Loop

Via nested loops figures like pyramids, triangles and others patterns can be printed.

Let's have a program which prints a rectangle with specific width and height

Output:

Any type of loop (while, do-while, for) can be nested inside other loop.