© Yanis Zafirópulos 2019
Yanis ZafirópulosSwift 4 Recipeshttps://doi.org/10.1007/978-1-4842-4182-0_3

3. Conditionals

Yanis Zafirópulos1 
(1)
Granada, Spain
 

Conditionals are at the very core of every programming language. A conditional statement allows us to perform different computations or actions depending on whether a specific Boolean condition evaluates to true or false, thus making changing program flow possible.

3.1 Write an if statement

Problem

I want to write a simple if statement.

Solution

var sunnyWeather = true
if sunnyWeather {
      print("Sure it's sunny. Let's go for a walk!")
}

How It Works

In its simplest form, the if statement has a single if condition. In a nutshell: it executes its block of statements only if that condition is true.
Sure ...

Get Swift 4 Recipes: Hundreds of Useful Hand-picked Code Snippets now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.