When would you use a do-while loop over a while loop?

  • When you want to create an infinite loop.
  • When you want to execute the loop body a fixed number of times.
  • When you want to execute the loop body at least once.
  • When you want to loop through a collection.
You would use a do-while loop over a while loop when you want to ensure that the loop body is executed at least once, regardless of whether the initial condition is true or false. The condition is checked after the first execution in a do-while loop.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *