Contents

Roo

The if construct

The if construct evaluates its branch of code only if its condition is truthy. If its condition is falsey it will evaluate its else branch (if present). You do not have to enclose the condition in parentheses (although you are free to do so if it improves readability).

var a = 1
if a > 0 {
    a = 5
}
print(a) # => 5

var b = 1
if b <> 1 {
    b = 5
} else {
    b = 10
}
print(b) # => 10

You can create a chain of if...else...if constructs like so:

var a = 3

if a == 1 {
    print("a == 1")
} else if a == 2 {
    print("a == 2")
} else if a == 3 {
    print("a == 3")
} else {
    print("a is something else")
}