Skip to content

Ternary Operator

IsaacShelton edited this page Mar 21, 2022 · 1 revision

Ternary Operator

The ternary operator can be used to evaluate different values based on a condition.

condition ? value1 : value2

Type Requirements

  • The condition must be convertible to bool
  • The first and second value must be of similar types

Short Circuiting

Only one of the two possible values will be evaluated. If the condition is true, the first will be, otherwise the second will be. Never will both be.

Usage Example

import basics

func main {
    name String = scan("Enter your name: ")
    print(name == "Isaac" ? "Welcome!" : "Hi there " + name + "!")
}
Clone this wiki locally