Nested For LoopsΒΆ

A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in rows and columns as shown below.

Note

The number of times a nested for loop body is executed is the number of times the outer loop executes times the number of times the inner loop executes. For the example above the outer loop executes 4-0+1= 5 times and the inner 9-0+1=10 times so the total is 5 * 10 = 50.

Check your understanding

Next Section - Common Mistakes