Computer Science Canada

java help - Loop Application

Author:  comp_help [ Wed Nov 18, 2009 11:07 pm ]
Post subject:  java help - Loop Application

Can someone please explain this question because I don't understand it:

For what kind of looping situations is a 'do' usually preferable to 'while'?

I am new to Java, so please try to keep it as simple as possible. Thanks.

Author:  Superskull85 [ Thu Nov 19, 2009 12:06 am ]
Post subject:  RE:java help - Loop Application

In a while (condition){...} loop the condition is always checked at the beginning of the loop. This kind of loop does not assure the programmer that it will run at least once (if the condition is false on the first check the loop will never execute).

In a do{...}while (condition) loop the condition is always checked at the end of a loop. This kind of loop does assure the programmer that it will run at least once (the loop will run, and than if condition is false the loop will exit).

Given that information can you think of a situation where you would want a loop to execute at least once?

Author:  comp_help [ Sat Nov 21, 2009 3:22 pm ]
Post subject:  RE:java help - Loop Application

Thanks for the explanation Superskull85.

I got: If a correct user input is required within the loop, then it?s best to use do-while loop, since it checks the condition in the end, meaning that the loop is run at least once.

Thanks anyways.


: