Conditions & Loops · Question 5 · Easy

A while loop keeps subtracting 2 from n. What is n when the loop ends?

n = 5
while n > 0:
    n -= 2
print(n)
  1. 0
  2. 1
  3. -2
  4. -1
Answer and explanation

Answer: D. -1

Explanation: n goes 5, 3, 1 and each of these passes the test n > 0. The third pass makes n equal to -1, the test fails, and the loop stops.

Previous: Which statement skips the rest of the current iteration and moves on to the… · Next: range() is given a negative step. What list does this print?

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