Understanding nested loops
Sometimes, we will need to iterate over another loop. To accomplish this, we use what are called nested or embedded loops. Essentially, a nested or embedded loop is a loop within a loop.
To conceptualize this, let’s look at some pseudocode:
For counter1 = 1 to 100 do Print "counter 1 is:" + counter1 For counter2 = 1 to 50 Print "counter 2 is: " + counter2 End_For End_For
If you’ve never seen a nested loop before, the output might be hard to picture. To alleviate this, consider the following theoretical output:
counter 1 is: 1 <- Outer loop iterates counter 2 is: 1 counter 2 is: 2 . . . counter 2 is: 50 counter 1 is: 2 <- Outer loop iterates counter 2 is: 1 counter 2 is: 2 . . . counter 2 is: 50 counter 1 is: 3 <- Outer loop iterates
As you can see in the...