Note: information on this page refers to Ceylon 1.1, not to the current release.

for statement

The for statement iterates the elements produced by an iterable object.


The general form of the for statement is:

for ( /* variable declaration */ in /* iterable expression */ ) {
    /* for block */
else {
    /* code executed if iteration doesn't exit early */

The else clause is optional.



A for statement accepts an expression of type Iterable, which is evaluated to produce an Iterable object.

The for block is executed repeatedly, with the iteration variable taking the value of each successive item produced by an Iterator obtained from the iterable object. Iteration of the loop terminated when the Iterator produces finished or an early exit occurs.

If an early exit does not terminate iteration of the loop, the else block is executed.

break and continue

Within the for block the break directive can be used to exit the block early without iterating over the remaining items in the Iterator. This is one form of early exit.

The continue directive can be used to skip execution of the remainder of the block and proceed with the next item produced by the Iterator.

Early Exit

If the for block ends with a return, break, or throw directive, the iteration is said to have exited early. In the case of return or throw, control is returned directly to the caller. In the case of a break statement, execution continues with the statement immediately following the for statement. Therefore, the else block is never executed in any case of early exit.

The else clause is ideally suited to situations where the for statement is being used to find something in a sequence or list, and the sought item has not been found:

Boolean definitelyInitialize(Person[] people) {
    Boolean minors;
    for (p in people) {
        if (p.age<18) {
            minors = true;
    else {
        minors = false;
    return minors;

Iterating entries

A special syntactic form of the for statement is provided for iterating instances of Entry.

for (key->item in map) {


  • Ceylon does not support the C-style for statement, with an initialising statement, iteration statement and termination condition. This isn't a problem in practice; see what the Tour has to say.

See also