Control Flow

Ternary Operator in Swift

The ternary operator is used to execute code based on the result of a binary condition.

It takes in a binary condition as input, which makes it similar to an 'if-else' control flow block. It also, however, returns a value, behaving similar to a method.


Syntax
result = binaryCondition ? valueReturnedIfTrue : valueReturnedIfFalse;

Notes

The ternary is strictly an assignment operator (cannot be used to execute expressions).


Example
let num1 = 3
let num2 = 5
var max = num1 > num2 ? num1 : num2

< Repeat-While Loop   |   Functions >

© 2019 SyntaxDB. All Rights Reserved.