Home » Swift Ternary Conditional Operator

Swift Ternary Conditional Operator

by Online Tutorials Library

Ternary Conditional Operator

The ternary conditional operator is used to shorten the code of if statement. It is a special type of operator with three parts i.e. question ? answer1 : answer2

It checks whether the question is true or false. If the question is true, it will return the value of answer1; otherwise, it will return the value of answer2.

Ternary conditional operator is a short form of the following code:

Let’s see the usage of ternary conditional operator in an example. In this example, we shall calculate the height for a table row. The row height should be 50 points taller than the content height if the row has a header and 20 points taller if the row doesn’t have a header.

Example (Without Ternary Conditional Operator)


Example (Using Ternary Conditional Operator)

Next TopicRange Operators

You may also like