• Post Reply Bookmark Topic Watch Topic
  • New Topic

java for loop  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain what kind of a java for loop is this:

or this one:



I am only familiar with this one:
 
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was a new kind of "for" loop introduced with Java version 5.0 (also known as version 1.5).

This new kind of for loop is often called the "for-each" loop because it loops over each of the elements of an array or loops over all of the elements that implement the Iterable interface (such as Collections).

Sun didn't want to introduce a new keyword such as "foreach" because that would have some backward-compatibility issues.

Sun's explanation for the new for-each loop.

Kaydell Leavitt
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
note: the loop you are "familiar" with uses semi-colons ; not commas ,
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To keep things clear, the new "foreach" loop uses colons.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!