Which of the following will not run the loop statement an infinite number of…

2023

Which of the following will not run the loop statement an infinite number of times in Python?

  1. A.

    while 21 % 2:

    print("ALWAYS")

  2. B.

    while 21 // 2:

    print("ALWAYS")

  3. C.

    while True:

    print("ALWAYS")

  4. D.

    while 1 % 1:

    print("ALWAYS")

Attempted by 2088 students.

Show answer & explanation

Correct answer: D

Correct conclusion: None of the given loop conditions prevent an infinite loop. Each condition evaluates to a truthy value, so all will run indefinitely unless explicitly broken.

  • while 21 % 2: print("ALWAYS") — 21 % 2 evaluates to 1; non-zero integers are truthy in Python, so this behaves like while 1: and is infinite.

  • while 21 // 2: print("ALWAYS") — 21 // 2 evaluates to 10; a non-zero integer is truthy, so this is also an infinite loop.

  • while True: print("ALWAYS") — True is always true, so this is an explicit infinite loop.

  • D: 1 % 1 = 0 → False → loop does not execute

✔️ Hence, Option D will not run infinitely.

Explore the full course: Rssb Senior Computer Instructor