O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter    16

Ternary Conditional Operator

You use the ternary conditional operator to evaluate a question and then do one of two things based on the result of the question. The ternary conditional operator is written like this: question ? action1 : action2 (see Listing 16-1).

The question is an expression that returns a boolean true or false. If the question returns true, then the first action takes place. If the question returns false, then the second action takes place.

Listing 16-1. Ternary Conditional Operator

let a = 5a == 5 ? "We're good" : "O0ps, not quite"

In Listing 16-1, the first part of the ternary conditional operator is the statement a ==5, which is using the equality comparison operator to test to see whether a is equal to 5. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required