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(1, 6): if i == 4: break else: print('done') print(i) ```
Answer: 4
The `break` at i==4 skips the `else` clause, and then `print(i)` outside prints 4.
What keyword is used to immediately jump to the next loop iteration in Python?
Answer: continue
`continue` transfers control to the next iteration of the enclosing loop.
How many times does 'hello' print? ```python for i in range(10): if i > 2: break print('hello') ```
Answer: 3
The loop prints 'hello' when i is 0, 1, and 2; at i==3 the `break` fires, giving 3 prints.
What is the result of: ```python count = 0 for _ in range(5): pass count += 1 print(count) ```
Answer: 5
`pass` does nothing, so `count += 1` executes for all 5 iterations, giving count == 5.
In a `while` loop, where must the `break` or `continue` statement appear?
Answer: Anywhere inside the loop body
`break` and `continue` can appear anywhere within the loop body, typically inside a conditional.
What is the output of: ```python for i in range(5): if i == 2: pass print(i) ```
Answer: 0 1 2 3 4
`pass` has no effect, so `print(i)` runs for every iteration including i==2.
Which of the following will create an infinite loop that can only be stopped with `break`?
Answer: while True:
`while True:` loops forever because the condition never becomes False; only `break` can exit it.