Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Enhanced Loop

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Ankit!
 
I will suppress my every urge. But not this shameless plug:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic