Swiftly

Swift 5.7 references for busy coders

Ternary conditional operator (_ ? _ : _)

The ternary conditional operator is a shorthand alternative to if-else.

Example if-else

if 5 > 6 {
  print("5 is more than 6")
} else {
  print("5 is not more than 6")
}
// Output: "5 is not more than 6"

Equivalent ternary

5 > 6 ? print("5 is more than 6")
      : print("5 is not more than 6")
// Output: "5 is not more than 6"

See also

Further reading