wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes do while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "do while loop" Watch "do while loop" New topic
Author

do while loop

Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35


One question,
is


do while (boolean) {}

equivalent to

while (boolean) {}


?
[ November 26, 2008: Message edited by: Clay Chow ]
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Originally posted by Clay Chow:


One question,
is


do while (boolean) {}

equivalent to

while (boolean) {}


?

[ November 26, 2008: Message edited by: Clay Chow ]


When you use do while it means statements inside the loop will be executed irrespective condition mentioned in while() is true or not. It will be executed at least once. But in case of while loop, statements inside the loop will be executed if and only if condition mentioned in while() is true.
Patel Chintan
Ranch Hand

Joined: Mar 01, 2007
Posts: 80
Originally posted by Patricia Samuel:


When you use do while it means statements inside the loop will be executed irrespective condition mentioned in while() is true or not. It will be executed at least once. But in case of while loop, statements inside the loop will be executed if and only if condition mentioned in while() is true.


int w = 0;
do while ( w < 1 )
System.out.print( "w is " + w );
while ( w > 1 ) ;

Is is similar to:
[edit]Add code tags. CR[/edit]
[ November 26, 2008: Message edited by: Campbell Ritchie ]

Chintan Patel, SCJP1.5, http://datewithjava.blogspot.com
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Did i misinterpret the question?

Wasn't Clay asking the difference between do while() and while()

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
That looks like a question from an exam; please quote where you got it from.
It is a confusing loop; it is actually an endless loop inside a do-loop. Patel Chintan: you got it right, but you ought to use the code tags, which I have added so you can actually read the code.

In these questions, they often quote incorrectly-indented code so as to confuse the examinees.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11168
    
  16

One question,
is

do while (boolean) {}

equivalent to

while (boolean) {}


no. taken as written, there is no such thing as a "do while (boolean) {}" construct. you can have a while loop:



or you can have a do-while loop:



in your example in the original post, what you have is a while loop inside a do-while loop:


[ November 26, 2008: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
wood burning stoves
 
subject: do while loop
 
Similar Threads
Box or Unbox
do while loop
do...while() statement question
Replace text by HTML links
null pointer