The for is a very versatile and powerful construct.The general format of the for statement
for(initialization;terminating condition;iteration){
// some code block
}
like all other statements if there is only one statement being repeated you can also avoid the curly braces.This structure is the one that java started with, but in jdk1.5 this has been extended further.Before going there I will first briefly describe about this because although the for loop is extended this format still remains to be quite popular.Let us start with each component of the loop statement:
initialization : This will typically contain the control variable(s) which will be acting as a counter to the loop we want.This is executed first by the compiler hence the control variables are also generally initialized at this point to limit their scope to the loop only.
Terminating condition : This is executed next and contains or rather sets at which point the loop must be exited. The condition on evaluation should return a boolean value to simply state weather the loop should proceed for another round or exit.
Iteration : At this point we will be altering(altering may be incrementing or decrementing or both) the control variable(s).This will occur after the loop has been executed once after the terminating condition has returned a true and there is no force exit in the loop.
Each block except the termination condition may consist of a single or multiple comma(,) separated statements.For example :
for(int i=0,j=13;i==j;i++,j--){
// some code block
}
is a valid for implementation while the following is an invalid statement and results in a compile time error
for(int i=0,j=0;i<3,j<3;i++,j++){
// some code block
}
Although I mentioned during the description of each of the individual components in the 'for loop' the need for each to act w.r.t the loop process is not strictly necessary i.e. the following are all valid examples of 'for' implementation as the components in a for loop are completely independent of each other
for( ; ; ){
// some code block
}
for(int a=0;b<7;a++){
b=a-2;
}
for( ;a<10; ){
// this is a for loop which acts like a while
}
From jdk1.5 though the for loop has been extended to cater to the needs of the arrays and collection objects. For which the syntax has been modified to accept only two instead of three components.The generalized form of the extended for loop for arrays is
for(declaration : expression){
// some code
}
The declaration should be a newly declared block variable that is compatible with the elements of the array that you will be accessing while the expression must evaluate to the array you will be looping through.The following examples should help
class test
{
public static void main(String[] args)
{
String sArray[] = {"hello","world"};
int towDiArray[][] = {{1,2,3}, {4,5,6}, {7,8,9}};
Long LArray[] = {4L, 5L, 6L};
//Legal extended array usages
for(String s : sArray){
System.out.println(s);
}
for(long l1 : getlongArray());
for(long l1 : LArray);
for(int n[] : towDiArray);
for(int n : towDiArray[2]);
}
public static long[] getlongArray(){
long lArray[] = {11L, 12L, 13L};
return lArray;
}
}
The expression can be a array variable or a function that returns an instance of the array and the arrays are not restricted to the predefined but also customized ones can be used.The declaration though cannot be a variable that is already defined but one that is freshly declared i.e.
int i;
int intArray[] = {1,2,3,4,5};
for(i : intArray);
throws and exception like
test.java:6: not a statement
for(i : intArray[2]);
^
test.java:6: ';' expected
for(i : intArray[2]);
^
test.java:7: illegal start of expression
}
Regarding the usage of the extended for loop for collections I will be dealing with it in the next post