Swift Ternary Conditional Operator: In this tutorial, you will learn to use the conditional or ternary operator to adjust the control flow of the program.
A ternary operator can be used to replace the if…else statement in specific situations.
Before you learn about the ternary operator, make sure to know about Swift if…else statement.
In this article, you will learn-
Ternary Operator in Swift
The ternary conditional operator is used to shorten the code of the if statement. It is a special type of operator with three sections for example question? answer1: answer2
It checks whether the question is true or false. In the event that the question is true, it will return the value of answer1; else, it will return the value of answer2.
A ternary operator evaluates a condition and executes a block of code dependent on the condition. Its syntax is
condition ? expression1 : expression2
Here, the ternary operator evaluates condition and
- if the condition is true, expression1 is executed.
- if the condition is false, expression2 is executed.
The ternary operator takes 3 operands (condition, expression1, and expression2). Consequently, the name ternary operator.
Example: Swift Ternary Operator
// program to check pass or fail let marks = 60 // use of ternary operator let result = (marks >= 40) ? "pass" : "fail" print("You " + result + " the exam")
Output
You pass the exam.
In the above example, we have used a ternary operator to check pass or fail.
let result = (marks >= 40) ? “pass” : “fail”
Here, if marks are greater or equivalent to 40, the pass is assigned to the result. Something else, fail is assigned to result.
Ternary operator rather than if…else
The ternary operator can be used to replace specific types of if…else statements. For instance,
You can replace this code
// check the number is positive or negative let num = 15 var result = "" if (num > 0) { result = "Positive Number" } else { result = "Negative Number" } print(result)
with
// ternary operator to check the number is positive or negative let num = 15 let result = (num > 0) ? "Positive Number" : "Negative Number" print(result)
Output
Positive Number
Here, the two programs give a similar output. Be that as it may, the use of the ternary operator makes our code more readable and clean.
Nested Ternary Operators
We can use one ternary operator inside another ternary operator. This is known as a nested ternary operator in Swift. For instance,
// program to check if a number is positive, zero, or negative let num = 7 let result = (num == 0) ? "Zero" : ((num > 0) ? "Positive" : "Negative") print("The number is \(result).")
Output
The number is Positive.
In the above example, we the nested ternary operator ((num > 0) ? “Positive” : “Negative” is executed if the condition num == 0 is false.
Note: It is recommended not to use nested ternary operators as they make our code more complex.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.