File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Which are the differences between the three (for, forEach and While)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Which are the differences between the three (for, forEach and While)?" Watch "Which are the differences between the three (for, forEach and While)?" New topic
Author

Which are the differences between the three (for, forEach and While)?

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Which are the differences between the three (for, forEach and While)? Could you give me examples of use?

Thank you,
André AS
cheng jian
Greenhorn

Joined: Jul 28, 2008
Posts: 7
All of them are used for loop.
Difference is
Example:
for:we use very often,it used to loop times is clear

print result:01234

forEach:we call it powerfull loop,it can be used to loop object

List<String> listStrings=new ArrayList<String>();

pring result:123

while:the loop has conditions

print result:01234

teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
for,for each,while are used for iterations.
for loop is used if you already know how many times the loop has to executed.
while is used when you do not know the number of times loop has to executed.
for each introduced in 1.5 version is used for iterating only the elements in arrays,collections.
ex:



SCJP 5
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might want to bookmark Sun's Java Tutorials for future reference. With respect to your question, see Learning the Java Language > Language Basics > Control Flow Statements.

Java's For-Each is also known as the "enhanced for statement," which was added with version 5. See The For-Each Loop.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which are the differences between the three (for, forEach and While)?
 
Similar Threads
Which are the differences between the three (String, BigDecimal and Integer formats)?
what is difference between foreach and for?
Web Server, Web Services and App Server
JSTL's EL. Problem iterating over arraylist
difference between three-tier and multi-tier