Ternary Operators are special use case scenarios for conditional statements. They basically simplify the if else conditional statements in Swift. They can be hard to read for new coders, but they should be used when necessary. Ternary conditionals contain three values — one comparative value, a second value that returns true, and a third that returns false based off the first condition. Here’s the following example below of turning a regular if-else conditional to a ternary conditional.
Ternary Operators are special use case scenarios for conditional statements.
In the example above, you can see the I’ve set constant (Value) to 15 and ran an expression if 15 is less than 17 print true, else print false. The ternary operator simplifies a few lines of code into one. “If value is less than 17 return true (pass) otherwise return false (fail). The result in the command line returns true. Please note that the question mark is used after the first expression and the colons are used between two return values that are the results.
Thank you for taking the time to read. Let us know in the comment section below if you have any questions in mind.