Question Detail

COS 102 6 lecture note; fundamentals of programming Objective Question

Question

In Python, when can the else statement be used in conjunction with a loop (for or while)?
Options
A Only if the loop contains a <code>break</code> statement.
B The <code>else</code> body will be executed when the loop's conditional expression evaluates to false.
Correct Answer
C It is not possible to use <code>else</code> with loops in Python.
D The <code>else</code> body will be executed before the loop starts.
Correct Answer

Option B is the correct answer.

Detailed Explanation

Page 84 states: 'else can be used in for and while loops the else body will be executed as and when the loop’s conditional expression evaluates to false.'

Hint

The else block in a loop is typically executed if the loop completes normally, without a break.

Question Info