iOS Development #10: If & Else Conditionals

iOS Development #10: If & Else Conditionals
Jan Zavrel
Jan Zavrel Follow July 12, 2021

A conditional is the ability to determine an outcome based on a given condition. We are used to making decisions based on certain conditions. For example, if we feel great, we’ll go for a walk, if we feel bad, we’ll stay in the bed.

Conditional is a powerful programming feature that brings us flexibility and complex behavior. It lets the program to react to certain changes automatically.

Conditionals handle for example the logic behind authentication when you’re signing into your social media website. If you’re logged in, you have access to a certain information and features, if you’re logged out, you are restricted from access to specific pages.

If Statement

When you need to execute a specific code only if a given condition is true, you will use the if statement.

Here is the structure of the if statement:

if condition {
  this code will run only if condition is true

Let’s see what’s going on here:

  1. The if keyword is always followed by a condition, the value of this condition must be either true or false.
  2. The condition is immediately followed by a code block enclosed in curly braces, { and }.
  3. The content of this code block is executed only when the condition is true.
  4. If the condition is false, the content of the code block won’t be executed.

Here’s an example of a simple authentication process. First, we declare the isLoggedIn variable and set its value to true.
Next, we set up the if statement that prints a message if the user is logged in:

var isLoggedIn: Bool = true 

if  isLoggedIn {  
	print("Welcome back!")  

Since the value of the condition is true, the message will be printed to the console.

Else Statement

An else statement is used to execute a block of code in the situation when the condition of an if statement is false. In this case, the else simply means, otherwise. You can read the whole command like this: Do this if a condition is true, otherwise, do something else:

if condition {
   this code will run only if condition is true
} else {
   this code will run only if condition is false 

There are two rules you need to remember:

  • An else statement can’t be used on its own. It can be only used in conjunction with an if statement.
  • An else statement does not accept a condition and is immediately followed by a code block.

Let’s use an else statement to complete the sign in logic and print a message to users who are not logged in:

var isLoggedIn: Bool = false  
if  isLoggedIn {  
	print("Welcome back!")  
} else {  
	print("Access Denied.")  

Since the initial value of the isLoggedIn variable is false, our condition is therefore false, and the program will run the code block for the else statement.

Notice how we use the indentation inside the if and else code block to make the whole code easier to read.

Else If Statement

So far, we’ve been using conditionals that can handle only one condition, but Swift allows us to add additional conditions to the basic if/else statement with the else if statement .

Here’s how it works:

if condition1 {
  this code runs only when condition1 is true 
} else if condition2 {
  this code runs only when condition2 is true 
} else if condition3 {
  this code runs only when condition3 is true 
} else {
  this code runs when all previous conditions are false 
  • The else if statement accepts a condition and a code block to execute for when that condition is true.

  • The else if statement exists only between an if and an else and cannot stand on its own like the if statement.

  • Any number of else if statements can exist between an if and an else.

Here’s an example of using multiple else if statements:

let temperatureInCelsius = 20  
let weatherComfort: String  
if  temperatureInCelsius >= 40 {  
	weatherComfort = "Unbearably hot"  
} else  if  temperatureInCelsius >= 30 {  
	weatherComfort = "I need a cold drink"  
} else  if  temperatureInCelsius >= 20 {  
	weatherComfort = "I'm fine in this room temperature"    
} else  if  temperatureInCelsius >= 10 {  
	weatherComfort = "I's a bit chilly"
} else  if  temperatureInCelsius >= 0 {  
	weatherComfort = "It's cold, I need a coat"
} else  if  temperatureInCelsius >= -10 {  
	weatherComfort = "It's freezing!"    
} else  if  temperatureInCelsius < -20 {  
	weatherComfort = "Unbearably cold"    
} else {  
	weatherComfort = "That depends on the current temperature..."    

Follow me on Medium, Twitter, Facebook and YouTube.

Jan Zavrel
Written by

Jan Zavrel Follow

Developer, Author, Teacher, Evernote Certified Consultant.