How to Avoid an Infinite Loop

Does your loop never stop?

Give it a condition that eventually becomes false.

How to Avoid an Infinite Loop

Things to check:

  • Make sure the condition changes
  • Increment your counter
  • Update variables used by the condition
  • Use break when appropriate
  • Avoid accidental while (true) loops

A loop continues while its condition evaluates to true.

If the condition stays true forever, the loop also runs forever.