Language / if / else

if / else

Use if / else for boolean branching.

In Silk, if is a statement that chooses which block executes. The broader language design includes expression-oriented forms; see the canonical spec for details.

Full reference: flow if else.

Notes#

Syntax#

if condition {
  // ...
} else {
  // ...
}

Examples#

Example: minimal if / else#

fn main () -> int {
  if true {
    return 0;
  } else {
    return 1;
  }
}

Example: else if chains#

else if parses as sugar for a nested if in the else branch.

fn main () -> int {
  let x: int = 1;
  if x == 0 {
    return 0;
  } else if x == 1 {
    return 1;
  } else {
    return 2;
  }
}

See also#

Source repository · Edit this page · View Markdown

Quick links

Type to search across the site. Use the up and down arrow keys to choose a result, Enter to open it, and Escape to close.