• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

old for loop vs new for loop

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the main diffrence here?

can anyone explain with a simple example?


regards ,
prasanna.s.k
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main difference is easier readability, at the cost of losing the iterator or array index.

Observe:

versus

So, when should you use the new loop? The answer: whenever you feel like it, and do not need the array index or iterator. I'm already using it in 95% of the cases.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It basically hides the iterator.

This example doesn't display the space saving it can sometimes buy you, but it will become apparent if you start using it.


[ November 20, 2007: Message edited by: Bill Shirley ]
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to the wonderfull and rocking replies.


it really helped


cheers,
Prasanna.s.k

Wish i could give a party for that!!!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but remember the for-each loop ("new for loop") is read-only. You can get data from the individual members of the array, but you can't writeCR
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can you write using the old for loop style? can you give a simple example







cheers
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can usually find the answer to that sort of question in the Java Tutorial.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic