File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and the fly likes Difference between if( and while( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Difference between if( and while(" Watch "Difference between if( and while(" New topic

Difference between if( and while(

Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
What is the difference between if( and while( under what circumstances are they implemented?

- Christian
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
"if" block is run only once (if the condition is satisfied). Whereas "while" block is run as long as the condition holds true.

MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
now coming back to your question:

when would i use if(

(a) To do something (only once) if the query returns atleast 1 row(s)
(b) To do something to the first row only

when would i use if(
(a) To do something to every row returned by the query

Hope this helps
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30076

For some more examples:

You usually use "while" as you want to loop through all the data in the result set. You use "if" when the query returns one row by definition. For example, "select count(*) from table."

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sreenivasan M

Joined: Oct 05, 2006
Posts: 10
Lovely lynette...good explanation
wood burning stoves
subject: Difference between if( and while(
Similar Threads
JRE vrs. JVM
checking resultsets
Quick Question HttpSessionBinding vs HttpSessionAttribute Listeners
Class.forName vs new
Run as AWT or SWT Application Eclipse Question