Q: How can you avoid an infinite loop in a while loop?
-
A
By ensuring the loop condition remains true
-
B
By including an increment or change to the loop control variable
-
C
By using the continue statement
-
D
By using the exit() function
B
Answer:
B
Explanation:
To avoid an infinite loop, it is important to ensure that the loop control variable changes with each iteration, which can be done by including an increment, decrement, or another modification inside the loop body. If the loop control variable does not change, the condition will never become false, leading to an infinite loop. The continue statement only skips the current iteration, and the exit() function is used to terminate the program, not just the loop.
Related Topic:
Share Above MCQ