if / else

if runs its block when the condition is true. Add else if to test further conditions, and a final else to handle everything that remains. The first matching block runs; the rest are skipped.

if x > 0 {
    Print("positive");
} else if x < 0 {
    Print("negative");
} else {
    Print("zero");
}

The braces are always required, even for a single statement, and the condition is written without parentheses.

The Condition Is a bool

An if condition must be a bool. Numbers and pointers are not implicitly truthy, so test them explicitly:

if count { }       // error: count is not a bool
if count != 0 { }  // OK

Combine several tests with the logical operators &&, ||, and !:

if age >= 18 && hasTicket {
    Admit();
}

The is type test also produces a bool, so it reads naturally as a condition:

if shape is Circle {
    let circle = shape as Circle;
    Print(circle.radius);
}

See Also