← flaviocopes.com

Swift Conditionals: ternary conditional

Published May 24 2021

⚠️⚠️ JUST A FEW HOURS LEFT to JOIN THE 2023 BOOTCAMP ⚠️⚠️

This tutorial belongs to the Swift series

The ternary conditional operator is a shorter version of an if expression. It allows us to execute an expression if a condition is true, and another expression if the condition is false.

Here is the syntax:

`condition` ? `value if true` : `value if false`

Example:

let num1 = 1
let num2 = 2

let smallerNumber = num1 < num2 ? num1 : num2 

// smallerNumber == 1

The syntax is shorter than an if statement, and sometimes it might make more sense to use it.



Wanna go from noobie to expert?

I wrote an entire book on this topic 👇

© 2023 Flavio Copes Flavio Copes using Notion to Site Notion to Site

Interested in solopreneurship?