Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
How many times does the line print("x") run?
Then say how many times it runs if the line is placed after the c loop, at the indent of for c.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. In nested loops, the number of times is the product of the numbers of rounds.
The three loops have 3, 4 and 2 rounds. The innermost body runs 3 * 4 * 2 = 24 times.
If the print("x") is placed at the indent of for c, it is the body of the b loop. It runs once for each round of a and b: 3 * 4 = 12 times.
Check. Run both versions, and count the lines of output.
Answer: 24 times, and then 12 times.
Log in to join the discussion.
Loading…