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
What is the output of: ```python for i in range(4): if i % 2 == 0: continue print(i) ```
Answer: 1 3
`continue` skips even numbers, so only odd values 1 and 3 are printed.
Which scenario would cause a `while` loop's `else` block to NOT execute?
Answer: A `break` statement exits the loop
The `else` block is skipped only when a `break` statement terminates the loop early.
What does `pass` do when used inside a loop body?
Answer: Does nothing; acts as a placeholder
`pass` is a no-op statement used as a syntactic placeholder where a statement is required.
What is printed by this code? ```python for i in range(3): for j in range(3): if j == 1: break print(i, j) ```
Answer: 0 1 1 1 2 1
The inner loop breaks when j==1, so j is 1 at that point; the outer loop prints i and j for each i.
In Python, can `break` be used outside of a loop?
Answer: No, it causes a SyntaxError
Using `break` outside a loop results in a SyntaxError because it has no loop to break out of.
What is the output of: ```python i = 0 while i < 5: i += 1 if i == 3: continue if i == 4: break print(i) ```
Answer: 4
When i reaches 4, `break` fires before the print, then `print(i)` outside the loop prints 4.
Which statement correctly describes the behavior of `for i in range(0):`?
Answer: The loop body never executes
`range(0)` produces an empty sequence, so the loop body executes zero times.