3 Replies - 2281 Views - Last Post: 21 September 2009 - 06:47 PM Rate Topic: -----

#1 jparkerson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-September 09

Do...while loop over while

Post icon  Posted 21 September 2009 - 06:32 PM

What situation would it be more appropriate to use a do...while statement than a while statement?
Is This A Good Question/Topic? 0
  • +

Replies To: Do...while loop over while

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Do...while loop over while

Posted 21 September 2009 - 06:36 PM

when you want the loop to execute at least once regardless of the loop's condition
Was This Post Helpful? 0
  • +
  • -

#3 jparkerson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-September 09

Re: Do...while loop over while

Posted 21 September 2009 - 06:40 PM

yes but what kind of situation would call for that?
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Do...while loop over while

Posted 21 September 2009 - 06:47 PM

for example if you want to keep taking input from the user until he inputs a negative number..here you want to execute the loop at least once
do{
int n = scanner.nextInt();

}while(n >= 0);



will break the loop once the user inputs a negative number..or for more sense maybe you would want to execute a block of statements in a loop until the users inputs N when the program asks him if he wants to continue or not

This post has been edited by mostyfriedman: 21 September 2009 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1