50.3k views
0 votes
To prevent a while loop from executing infinitely, which of the following actions must not occur?

a. A loop control variable is initialized to a starting value.
b. The loop control variable is tested in the while statement.
c. The loop control variable is altered within the body of the loop.
d. The loop control variable is reset to its starting value during each iteration of the loop.

User Waltwood
by
7.8k points

1 Answer

3 votes

Final answer:

To prevent an infinite loop, the loop control variable should not be reset to its starting value during each iteration. The correct action that must not occur is option d.

Step-by-step explanation:

To prevent a while loop from executing infinitely, one must ensure that the loop control variable does not return to its starting value within the body of the loop after each iteration. This is encapsulated in option d: The loop control variable is reset to its starting value during each iteration of the loop. Doing so would cause the while loop to continue indefinitely because the condition to exit the loop would never be met. Proper utilization of a while loop typically includes the following three steps:

  • The loop control variable is initialized to a starting value before entering the loop.
  • The loop control variable is tested within the while statement to determine if the loop should continue executing.
  • The loop control variable is altered within the body of the loop in a way that will eventually meet the exit condition.

User Cronfy
by
7.9k points