For Loops - Extenders
 

For loops


//for loop syntax:
int x;
for (x=0; x < 50; x++) {
     System.out.println("Hi There!");
}

//broken down into three parts

//part 1: the first part x=0; is called the initialization
//sets x to 0; executes at start of loop, does not execute again

//part 2: the condition x < 50; executes every time through the loop
//checks to see if the condition is still true, if false, loops terminates
//if condition true, body of loop which prints "Hi There!" executes

//part 3: x++ - which adds one x, this occurs after the body of the for loop
//executes which prints "Hi There!" - after x++, the condition in part 2 above
//is checked again


Example of for loop with extensions:
(A shortcut way to loop through Arrays & ArrayLists using For)
Example:

ArrayList<Actor> actors = new ArrayList<Actor>;

for (Actor currentOne : actors) {
     currentOne.removeSelfFromGrid();
}

//first, an ArrayList called actors which contains Actor objects
//is created -- note the <Actor> specification for ArrayList meaning
//the List only contains Actor object.
//we then want to go through the ArrayList actors and do something to
//each element in the list, we could use: for (x=0; x < actors.size(); x++)
//and then in the body of the for loop execute: Actor v = actors.get(x);
//then v.removeSelfFromGrid();
//but the new version of Java has a built in shortcut or extension
//so that for loops and lists/arrays are easier to code..
//the code for (Actor currentOne : actors)
//translates to: loops through ArrayList actors
//and assign each time through list the current Actor item in ArrayList
//object reference, labeled currentOne