Tuesday, August 2, 2011

When is it good to use a do-while loop? (java)?

You would run a do-while loop when you want the first iteration of the loop to be executed regardless of the condition, and then the following iterations will only execute if the condition is true. For example, if you are asking someone to enter a positive integer, you would want a do-while loop, the condition being that the number the user typed was positive. A while loop would not work in this case because there would be no number to check because the condition is checked before the loop starts.

No comments:

Post a Comment