if statement

The if statement allows a block of code to be executed conditionally.


The general form of the if statement is:

if ( /* some conditions */ ) {
    /* code executed if some conditions are true */
} else if (/* other conditions */ ) {
    /* code executed if other conditions are true */
} else {
    /* code executed otherwise */
/* code after if statement */

There can be zero or more else if clauses, and the else clause is optional.


Ceylon's if statement should already be familiar to anyone who has programmed using a C-like language.


The if condition is evaluated first, and if it is satisfied then execution proceeds with the following block of code, and after that with the code after the if statement.

Otherwise, if the if condition is not satisfied, then the first else if condition, if any, is evaluated and if that condition satisfied then its associated block is executed, followed by the code after the if statement. If the else if condition is not satisfied, then subsequent else if clauses, if any, are treated in the same way.

Finally, if none of the conditions are satisfied, the else block, if any, is executed, followed by the code after the if statement.


The conditions in an if statement occur in condition lists.

Any expression of type Boolean may occur in the condition list of an if statement. The if statement also supports the use of typing conditions:

These conditions narrow the type of a reference within the associated block, and in later conditions in the condition list and the rest of the control structure.

void printSqrt(Object x) {
    if (is Float x, x >= 0.0) {

By not separating the operation that checks the safety of the typecast from the operation used to actually perform the typecast Ceylon eliminates the possibility that the programmer might forget to do the test before attempting the typecast, and eliminates repetition of the narrower type.

Flow typing can also affect the rest of the control structure:

void go(Car|Bicycle vehicle) {
    if (is Car vehicle) {
        // vehicle has type Car
    } else {
        // vehicle has type Bicycle

Destructuring in exists and nonempty conditions

Since Ceylon 1.3, exists and nonempty conditions support destructuring. For example:

if (nonempty [name, *rest] = process.arguments) {
    print("Hello ``name``!");

See also