1

I've come across a for loop structured in a way I've never seen before. I'm wondering if you can explain to me what it is doing? It is provided as one of the examples for verlet integration in Processing:

http://www.openprocessing.org/sketch/17191

Here is the code:

for(VerletParticle2D p : physics.particles) { ellipse(p.x, p.y, 5, 5); } 

Is it simply adding an 'p' particle until it reaches the amount that has been setup before?

6
  • What have you already searched for before posting the question? Commented Apr 11, 2012 at 16:52
  • 5
    @bluesman: In fairness to the OP, it's pretty hard to google for unfamiliar syntax, especially one that involves no special keywords. Commented Apr 11, 2012 at 16:57
  • @aix +1 for general truth, but second result by just typing "java for loop": java documentation, where you'll learn that Oracle calls this an "enhanced for loop". Commented Apr 11, 2012 at 17:19
  • @MarioDeSchaepmeester I skipped just 'java for loop' as I thought that would only return a normal for loop. Guess I was wrong. Commented Apr 11, 2012 at 17:25
  • @Schmooo and that's where aix is right by saying it's difficult to google for unknown syntax. The java for loop is what you knew there was, so you thought that search would only make you find those. I never downvoted you btw :) Commented Apr 11, 2012 at 17:42

4 Answers 4

7

It's the so-called "for each" loop. It simply iterates over all elements of the collection (or array) physics.particles, assigning each element in turn to p.

For further information, see Oracle documentation.

Sign up to request clarification or add additional context in comments.

Comments

3

This is the Java "For-Each" loop. It iterates over all elements in a collection.

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

Comments

3

That is a for-each loop. It iterates over a collection.

In this case, the collection is physics.particles. p will represent the current object in each iteration. VerletParticle2D is the compiler type of the object.

Comments

3

The for loop is iterating through the "Particles" in physics.particles and for every element in it, it is calling the ellipse function call.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.