Question presented to candidate:
"Walk me through break, continue and pass. Then tell me: Python loops can have an else clause β when does it run, and when would you actually use it?"
What a strong answer should cover:
breakleaves the innermost loop immediately;continueskips the rest of the current iteration and goes back for the next item (or re-tests thewhilecondition).passdoes nothing at run time; it only fills a place where the grammar needs a statement (empty function, class, or branch).- The loop
elseruns when the loop finishes withoutbreakβ including a loop that runs zero times β and is skipped bybreak,returnor an exception. - The idiomatic use: a search loop, where
elsemeans "not found", instead of a separate flag variable. breakonly exits one loop level; to stop nested loops use a function withreturn, a flag, or the for-else-continue idiom.finallystill runs whenbreakleaves atry.
Code / implementation expected: Optional β a short search loop with for ... else, or a nested-loop exit.