About
The for statement is a part of the control flow possibility of Java.
The Iterable interface must be implemented in order to use it. See for example: Java - Iterator Implementation
Property
Index
During a for loop, this is no shortcut syntax to get the index. You need to create it
int index = 0;
for (String s: strings){
System.out.println("The current index is "+index);
assert s == strings.get(index): "Error";
// Increment
index++
}
Syntax
General
for (initialization; termination; increment) {
statement(s)
}
Enhanced
for (datatype variable : collections or array) {
statement(s)
}
Infinite
for ( ; ; ) {
// your code goes here
}
Skip
The continue statement skips the current iteration of a for loop.
Example
Counter
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
Array
int[] anArray= {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < anArray.length; i++) {
System.out.println(i+" : "+anArray[i]);
}