If/Else Conditionals

This is what if/else conditions look like in Shim:

let a = 5
if a > 10 {
    print("big")
} else if a > 5 {
    print("medium")
} else {
    print("small")
}
Press the play button to execute

In the above example, the a > 10 expression is called a “predicate”. Shim will run the contents of the {} block after the predicate if the predicate is truthy. Empty collections (like lists) are falsy, but values like true, 42, and ["non", "empty", "list"] are truthy.

Unlike some other languages, this predicate doesn’t need to be enclosed in brackets. Additionally, the {} block is always required (unlike some C-like languages where you can have a single statement without needing the {} block).

If-Else as Expressions

let description = if a > 10 {
    "big"
} else if a > 5 {
    "medium"
} else {
    "small"
}
print("The number is \(description)")
Press the play button to execute

If no else branch is given, then the expression evaluates to None.

let a = if false {"test"}
print(a)
Press the play button to execute