Question Detail

BIO 107 While Loops & Control Flow Objective Question

Question

Consider the code snippet: i = 1; while i <= 5: print(i) # FORGOT i = i + 1!. What type of loop will this create?
Options
A A loop that runs 5 times and stops.
B A loop that prints 1 once and stops.
C An infinite loop.
Correct Answer
D A syntax error.
Correct Answer

Option C is the correct answer.

Detailed Explanation

As indicated by the comment '# FORGOT i = i + 1!', the variable 'i' is never updated. Therefore, 'i' remains 1, the condition 'i <= 5' (1 <= 5) is always True, resulting in an infinite loop that continually prints 1.

Hint

The crucial part of avoiding an infinite loop is the 'update variable' step.

Question Info