2.11 Nested Iteration

19 阅读1分钟

1. Exam Points

  • Nested iteration (while in while, for in for, while in for, for in while)
  • Count the number of times a statement is executed.
  • Note: Analyze nested loops
    • Analyze how the outer i changes
    • Analyze how the inner j changes with the outer i
    • Analyze what is printed or executed in the inner loop
  • Note:
    • Pay attention how the loop control variables of the inner and outer loops changes. (++, --, +=2, -=2).
    • When printing multiple lines using nested loops:
      • the outer loop controls how many lines are printed,
      • the inner loop controls what is printed for each line.
      • Rule out wrong options first if possible.
      • What is printed in the loop (outer i or inner j)

2. Knowledge Points

(1) Nested Iteration

  • Nested iteration statements are iteration statements that appear in the body of another iteration statement.
  • Example 1:
    image.png
    • Analyze nested loops
        1. Analyze how the outer i changes (1~6)
        1. Analyze how the inner j changes with the outer i (1~i)
        1. Analyze what is printed or executed in the inner loop (dog)
  • Example 2:
    image.png

3. Exercises