Conditions & Loops · Question 14 · Medium
When does the else block attached to a for loop run?
- Only when the loop body is never executed
- Only when the loop is ended by break
- When the loop finishes all its iterations without executing break
- 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
- zip() pairs two lists of unequal length inside a loop. What total is printed?
- The inner loop's range depends on the outer loop variable. How many times does…
- A helper function prints its argument each time it is called inside and / or…
- A while loop halves even numbers and applies 3n + 1 to odd numbers until n…
- break appears inside the inner of two nested loops. Which pairs are collected?
- A while loop with an else clause tests whether 91 has a small divisor. What…
- The loop variable is changed inside the body of a for loop. What does this…
- Both conditions of this if-elif contain a division by x, and x is 0. What is…
- continue comes after the increment inside a while loop. What list is built?
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