Conditions & Loops · Question 14 · Medium

When does the else block attached to a for loop run?

  1. Only when the loop body is never executed
  2. Only when the loop is ended by break
  3. When the loop finishes all its iterations without executing break
  4. Once after every iteration of the loop
Answer and explanation

Answer: C. When the loop finishes all its iterations without executing break

Explanation: A loop's else block runs when the loop ends normally, that is, the sequence is exhausted with no break. It also runs for an empty sequence, but that is not the only case. If break executes, the else block is skipped.

Previous: A helper function prints its argument each time it is called inside and / or… · Next: A while loop halves even numbers and applies 3n + 1 to odd numbers until n…

Practise conditions & loops

Try a fresh set of conditions & loops questions with an explanation after every answer, free and without signing up: Conditions & Loops practice. Or sit a full placement mock test.

More conditions & loops questions

More python practice

Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge