Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A quality-control engineer stores defect codes in a list `codes` and wants to print each code until a sentinel value `'STOP'` is encountered, at which point the loop should end without printing `'STOP'`. Which loop correctly achieves this behavior?

⚠ Common exam trap

Many candidates confuse break with continue, or placing the print before the sentinel check so the sentinel is printed despite the intention to stop before it.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

for c in codes: if c == 'STOP': break print(c)

The correct loop checks each element for the sentinel before any output and uses break to exit the entire loop. Placing the break before the print ensures the sentinel is never displayed, and break ends iteration immediately so no later codes are processed. This matches the engineer's requirement precisely.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    for c in codes: if c == 'STOP': continue print(c)

    Why it's wrong here

    Using continue skips only the current iteration when 'STOP' is seen; the loop then proceeds to any remaining elements. This means codes after 'STOP' would still be printed, and the loop does not end at the sentinel. The scenario requires the loop to stop entirely, not merely skip the sentinel.

  • ✓

    for c in codes: if c == 'STOP': break print(c)

    Why this is correct

    This loop iterates over the codes, checks for the sentinel before printing, and breaks immediately when 'STOP' appears. Because the break occurs before the print statement, 'STOP' itself is never printed. It meets the requirement of printing each code until the sentinel and then terminating the loop cleanly.

  • ✗

    for c in codes: while c != 'STOP': print(c)

    Why it's wrong here

    The inner while loop never changes c, so if the first element is not 'STOP' it prints that same value forever, creating an infinite loop. If the first element is 'STOP', nothing prints and the outer for advances, but the logic is still broken. This does not correctly process the list or terminate at the sentinel.

  • ✗

    for c in codes: print(c) if c == 'STOP': break

    Why it's wrong here

    Here the print happens before the sentinel check, so 'STOP' is printed before the loop breaks. The scenario explicitly requires that 'STOP' not be printed, so this order of statements produces output that includes the sentinel and fails the requirement even though the loop does terminate.

About these practice questions

This PCEP question is part of Courseiva's 482-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.