Q: What happens if you forget to include a condition that changes in a while loop?
-
A
The loop will skip iterations
-
B
The loop will terminate after the first iteration
-
C
The loop will run infinitely
-
D
The loop will throw an error
C
Answer:
C
Explanation:
If a condition in a while loop is based on a variable that never changes inside the loop, the condition will always evaluate to true, and the loop will run infinitely. This is a common error in programming, especially for beginners. It is important to include some mechanism inside the loop (such as an increment or modification of the control variable) to eventually make the loop's condition false, allowing the loop to terminate properly. Infinite loops can crash programs, consume unnecessary CPU cycles, and cause other issues.
Related Topic:
Share Above MCQ