โ† All PCEP Flashcard Decks

Controlling loop execution Flashcards

7 cards from real PCEP practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 7 Controlling loop execution flashcards as text
  1. What is the output of: ```python for i in range(3): for j in range(3): if i == j: continue print(i, j) ```

    Answer: Prints all pairs where i != j

    `continue` skips printing when i equals j, so only pairs where i != j are printed.

  2. What is the final value of `total` after this code? ```python total = 0 for i in range(1, 10): if i % 2 == 0: continue total += i print(total) ```

    Answer: 25

    Only odd numbers 1,3,5,7,9 are summed (evens are skipped), totaling 25.

  3. What happens to the loop variable after a `for` loop completes normally in Python?

    Answer: It retains the last value assigned during iteration

    In Python, the loop variable persists in the local scope with its last assigned value after the loop ends.

  4. What is printed by: ```python x = 5 while x > 0: x -= 1 if x == 2: continue print(x) ```

    Answer: 4 3 1 0

    When x becomes 2, `continue` skips the print, so 4, 3, 1, and 0 are printed.

  5. Which of the following is a valid reason to use a `while True:` loop with `break`?

    Answer: When the exit condition is complex and easier to check mid-body

    `while True:` with `break` is useful when the termination condition is best evaluated in the middle of the loop body.

  6. What does this code output? ```python for i in range(1, 4): print(i) else: print('end') ```

    Answer: 1 2 3 end

    The loop prints 1, 2, 3 and since no `break` occurs, the `else` clause prints 'end'.

  7. What is the output of: ```python for i in range(5): if i == 3: break else: print('no break') print('after') ```

    Answer: after

    The `break` at i==3 prevents the `else` from running, but `print('after')` outside the loop executes.