.


Divya

Java for loop statement


java for loop

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
}

Example

class for loop Example {
public static void main(String[] args) {
for (int a = 1; a<= 10; ++a) {
System.out.println( a);
}
}
}

Output :
1
2
3
4
5
6
7
8
9
10

java for each loop

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
}

Example

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);
}
}
}

Output :
z
t
u
v
w



.