Skip to content

Latest commit

 

History

History
119 lines (89 loc) · 1.64 KB

for.md

File metadata and controls

119 lines (89 loc) · 1.64 KB

For statement

Overview

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.

  1. The initialization field.
  2. The condition field.
  3. The increment field.

The initialization 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);

Infinite loop

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

Examples

Example 1. Normal case

Code

var i;
for (i = 5; i < 10; i++) {
    System.println(i);
}

Result

5
6
7
8
9

Example 2. Infinaite loop

Code

var i = 0;
for ( ; ; ) {
    if (i > 1000) break;
    ++i;
}
System.println(i);

Result

1001

Example 3. Declaration variable in scope

Code

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

Result

10
100