Skip to content
Kavachकवच

If...Else: if as a value

● Beginner2 min

An if is a value: it picks between two values in one line.

fn main() {
    const paid = false
    const label = if paid { "paid" } else { "due" }
    print(label)
}

The program ran and printed:

kavach run value.kav

due
value.kav: Kavach code, then what running it prints

Each block's last line is its value. This replaces the ? : operator of other languages, with words a beginner can read. Both branches must give the same type.