• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enhanced Loop

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to know the working of Enhanced loop in a simple method.
I did understand it once but then i forgot
Does any one has a simple way so i can understand it and that it stays in my brain!?!
There were various posts on it, but i can't find it.
If some one has the link please pass it on.

This was a small code i wrote.
I can't understand the second part of the result.
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enhanced for loop does one and only one thing, looping through all elements of an given array or Iterable object.

It is like, saying "for each object in the given array or iterable object, do something". While looping through the array or Iterable object, assigns the currently positioning element into given variable so that you access the element in the loop body.

Back to your code:

In the first loop, you are looping through the array and printing the array values.

In the second loop, you are looping through the array, using the values as index to select the array values.

First iteration, var i would be "9" and so array value at position "9" is "0"; That's what you are printing.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh Thanks!

Looks like i was thinking more then i should!!

There was a post about some mock question which used an Enhaced Loop and where the result was 0030 or something of that sort.
Ther was a long discussion on it.

Actually there were 2 posts on it.

Doesn any one has a link to it?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey I don't know where that post is but the code was this

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit!
 
reply
    Bookmark Topic Watch Topic
  • New Topic