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.

result = binaryCondition ? valueReturnedIfTrue : valueReturnedIfFalse;


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

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

< Repeat-While Loop   |   Functions >

© 2019 SyntaxDB. All Rights Reserved.