MCQ Single Best Answer Moderate

QHow can you avoid an infinite loop in a while loop?

ID: #23088 While Loop in Programming Language 84 views
Question Info
#23088Q ID
ModerateDifficulty
While Loop in Programming LanguageTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • 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
Correct Answer: Option 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.

Share This Question

Challenge a friend or share with your study group.