for
statement is a pre-test loop with 3 fields, which are initialization, increment, and a condition check.
The condition will be evaluated at the head of loop.
for (var i = 0; i < 10; ++i) {
// loop 10 times.
}
for
statement has 3 fields as below.
- The initialization field.
- The condition field.
- The increment field.
In the initialization field, you can write any expression or declaration.
If it is a declaration, the variable is available only in for
scope.
var i, j = 100;
for (i = 0; i < 10; i++) {
// code.
}
// The variable `i` is alive.
System.println(i);
for (var j = 0; j < 10; j++) {
// code.
}
// The variable `j` can be no more accessed.
System.println(j);
If you put nothing at all fields, it means infinite loop.
break
statement is needed to exit an infinite loop.
for ( ; ; ) {
// infinite loop.
if (codition) break;
}
var i;
for (i = 5; i < 10; i++) {
System.println(i);
}
5
6
7
8
9
var i = 0;
for ( ; ; ) {
if (i > 1000) break;
++i;
}
System.println(i);
1001
var i, j = 100;
for (i = 0; i < 10; ++i) {
// code.
}
// The variable `i` is alive.
System.println(i); // => 10
for (var j = 0; j < 10; j++) {
// code.
}
// Loop counter `j` is scoped out.
System.println(j); // => 100
10
100