← 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 will be the output of the following code snippet? ```python for i in range(5): if i == 3: break print(i, end=' ') ```

    Answer: 0 1 2

    The loop iterates through numbers from 0 to 4. The `print` statement executes for `i` values 0, 1, and 2. When `i` becomes 3, the `if` condition is met, and the `break` statement is executed, which terminates the loop immediately.

  2. What is the output of this code? ```python for i in range(5): if i == 3: continue print(i, end=' ') ```

    Answer: 0 1 2 4

    The loop iterates from 0 to 4. When `i` is 3, the `if` condition is true, and the `continue` statement is executed. This statement skips the rest of the current iteration (the `print` statement) and proceeds to the next iteration (when `i` is 4).

  3. What does the following code print? ```python i = 0 while i < 5: print(i, end=' ') i += 1 if i == 3: break ```

    Answer: 0 1 2

    The `while` loop starts with `i = 0`. It prints 0, then increments `i` to 1. It prints 1, increments `i` to 2. It prints 2, increments `i` to 3. Now the condition `i == 3` is true, and the `break` statement terminates the loop.

  4. What is the output of this `while` loop? ```python i = 0 while i < 5: i += 1 if i == 3: continue print(i, end=' ') ```

    Answer: 1 2 4 5

    Inside the loop, `i` is incremented first. When `i` becomes 3, the `continue` statement is executed, skipping the `print(i)` for that iteration. The loop continues, printing the values 1, 2, 4, and 5.

  5. What is the output of the following nested loop? ```python for i in range(2): for j in range(2): if i == j: break print((i, j), end=' ') ```

    Answer: (1, 0)

    The `break` statement only affects the innermost loop it is in. When `i` is 0, the inner loop runs for `j=0`. The condition `i == j` is true, so `break` is executed, and nothing is printed. When `i` is 1, the inner loop runs for `j=0`. The condition is false, so `(1, 0)` is printed. Then `j` becomes 1, the condition `i == j` is true, and the inner loop breaks.

  6. What will be printed by this code? ```python for num in range(4): if num == 2: break else: print("Done") print(num) ```

    Answer: 2

    The `for` loop's `else` block is executed only if the loop completes its iterations without being terminated by a `break` statement. In this case, the loop breaks when `num` is 2. Therefore, the `else` block is skipped. The final `print(num)` statement prints the last value of `num` before the loop was exited, which is 2.

  7. What is the output of the following code? ```python for i in range(2): print(f"Outer {i}") for j in range(2): if j == 1: continue print(f" Inner {j}") ```

    Answer: Outer 0 Inner 0 Outer 1 Inner 0

    The `continue` statement only skips the current iteration of the inner loop. For each outer loop iteration (`i=0` and `i=1`), the inner loop starts. When `j` is 0, it prints. When `j` is 1, the `continue` skips the print statement and the inner loop finishes, then the outer loop proceeds to its next iteration.

How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. Exam facts are checked against the official sources listed below. Not reviewed by a subject-matter expert. Editorial policy

Sources & references

Exam facts on this page (fees, format, dates, passing scores) are checked against these official sources; the date is our last check. Editorial policy

  1. pythoninstitute.org — Fees, exam format, passing score, retake fee, score release, time limit, score validity, test delivery, eligibility, retake policy · verified Oct 2026