12 Iteration – Nested Loops

Nested loops[1] are loops inside other loops.

Note that the outer loop[2] (first_word) is run first and then the code block encounters the second inner loop (second_word).

The inner loop is run three times and then moves onto the code below the indented for loop.

Then the code block for the outer loop finished and it gets the second item in the outer loop and runs the code block again.

This repeats until the all of the items have been iterated (looped) through.

first_word = ["Play", "Master", "Super"]
second_word = ["Station", "System", "Console"]

for x in first_word:
  print("Start of", x)
  for y in second_word:
    print(x, y)
  print("End of", x)
Terms definitions
1. Nested loops. Just like “nested dolls” sit within each other, loops must sit inside each other completely, or they don’t work.
2. loop. A sequence of instructions in a program repeated until a certain condition is reached: * with a “for loop”, the instructions are repeated a certain number of times * with a “while” loop, the instructions are repeated until a given condition becomes false * with a “forever loop” (also known as an “infinite” or “endless” […]