In java for loop is a control flow statement for specifying iteration.
It allows code to be executed repeatedly.
for (statement 1; statement 2; statement 3) {
code block to be executed
}
class for loop Example {
public static void main(String[] args) {
for (int a = 1; a<= 10; ++a) {
System.out.println( a);
}
}
}
When accessing collections, a foreach is significantly faster than the basic for loop's array access.
It is usually used in place of a standard for statement.Unlike other for loop constructs.
The foreach statement in some languages has some defined order, processing each item in the collection from the first to the last.
for(Type var:array){
//code to be executed
}
class for each loop Example {
public static void main(String[] args) {
char[] letter = {'z', 't', 'u', 'v', 'w'};
// foreach loop
for (char a: letter) {
System.out.println(a);
}
}
}