File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes do... whiles are good... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "do... whiles are good..." Watch "do... whiles are good..." New topic

do... whiles are good...

Richard Whitely

Joined: Feb 08, 2002
Posts: 2
In the Programming Style Guide area of JavaRanch it states:

"Never use a do...while"
I belive that there are certain situations where a do...while is good e.g. examining a resultset (which isn't scrollable).

Example with do...while

Example without do...while

Any opinions are welcome... especially any equaly attractive sollutions which dont ude do...while.
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
I am inclined to agree. I don't think this should be a hard and fast rule. Sometimes logic dictates that this is the correct construct.
However the reasoning for the rule in JavaRanch is valid.
The way I get round this is by placing the code for the do while in a method, therefore it is still possible to see from the start of the loop what the termination condition is.

Very occasionally I will stretch this to two or three method calls, but never any more than that. I know this makes it hard to define a "rule", maybe it should be a guideline.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I tend to agree with the Ranch on this one...just for consistency. This is how I work the code so that it works for me:

since I usually need the row count as well, there is no real performance cost of incrementing the counter. also to note, that incrementing a counter is hardly a performance cost especially when you start making hits to the database!
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9058

Another choice

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Your name doesn't agree with the javaranch guidelines. Please take a moment and edit your profile after reviewing the guidelines at
Please change your Publicly Displayed name to follow the guidelines.
thanks for your cooperation.
- satya

Take a Minute, Donate an Hour, Change a Life
I agree. Here's the link:
subject: do... whiles are good...
jQuery in Action, 3rd edition