Branching IF Statements in Swift

If-Then-Else

Execute code depending on a conditional statement? Why not?

Why? Because it is a basic building block of coding. You’re not sure how to do it?

Read on and find out how this is implemented in Swift!

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)

OR

  • Use Swift Playgrounds to follow with the code (guide HERE)

PLUS

  • Some understanding of logical operators in Swift (guide HERE)

Terminology

Branching: An instruction that tells a computer to begin executing a different part of a program rather than executing statements one-by-one

Compiler: A program that converts instructions into a machine-code or lower-level form so that they can be read and executed by a computer

Conditional statement: A statement that is created by the programmer which evaluates actions in the program and evaluates if it is true or false

IF statement: A decision-making statement

Playgrounds: A development environment that can be used to create Swift code

Statement: A command the programmer gives to the computer through a compiler

Swift: An open source programming language for macOS, iOS, watchOS and tvOS

Variable: A label for a memory location

Make your statement

You might have any individual statement that you want Swift to execute. There are lots of relatively simple examples, but I can give you one that you might accept.

print (1 + 1)

This may surprise, or even shock you. Swift will print out the number 2 to the console. In playgrounds that would look something like the following:

Image for post
Image for post

But we want to produce some more sophisticated code. We want to react according to the user’s input, or perhaps some other variable or value that might arise in our code (something like a random event).

One solution to this?

Branching. Read on for the information.

Branching with IF Statements

Branching using IF statements is a great way to have decisions play themselves out within your code.

The straightforward IF statement

// Code here would run no matter if the condition statement is true or falseif (conditional statement) {
// Do something
}
// Code here would run no matter if the condition statement is true or false

so the code within the brackets {} will run if the condition is met.

A rather helpful diagram is shown below to show you how this works:

Image for post
Image for post

We can call the diagram above the IF-Then pattern for IF statements.

The straightforward IF-Then-Else statement

There might be some cases if a Conditional statement is met you want to run some set of actions, if not run another set.

// Code here would run no matter if the condition statement is true or falseif (conditional statement) {
// Do something
} else {
// Do something if the conditional statement is false
}
// Code here would run no matter if the condition statement is true or false

This is IF-Then-Else diagram below

Image for post
Image for post

Additional else clauses

As some form of inception, an IF statement can contain another IF statement. This allows you to have multiple conditional statements kind of in one.

// Code here would run no matter if the condition statement is true or falseif (conditional statement) {
// Do something if the conditional statement is true
} else if (second conditional statement) {
// Do something if the second conditional statement is true
} else {
// Do something if both the first and second conditional statements are false
}
// Code here would run no matter if the condition statement is true or false

This results in the following diagram:

Image for post
Image for post

The examples

A real-world example of IF-Then-Else

If we take the 24-hour clock the hour is larger than 18 the time is known as “evening” (check the clock below for details), if not it is “not evening”.

An IF statement will help up with this.

Image for post
Image for post

Let us try this example with an hour of 23

var hour = 23:00
if (hour >= 18) {
print ("Evening")
}
else {
print ("Not Evening")
}

Excellent!

A slightly more complex real-world example of IF-Then-Else

let us think — we might restrict the evening in our example above to between the hours of 18 and 22. An IF statement can have a condition with && (representing the logical AND) in between!

var hour = 23:00
if (hour >= 18 && hour <= 22) {
print ("Evening")
}
else {
print ("Not Evening")
}

Additional else clauses

Swift can really help you out by having kind of stacked IF statements. Check out the following example:

var hour = 23:00
if (hour >= 18 && hour <= 22) {
print ("Evening")
}
else if (hour <= 00 && hour <= 11 ) {
print ("Morning")
} else {
print ("A different time")
}

Conclusion:

Control statements are extremely important in any programming language. Through this article you have seen how IF statements can be used in Swift, with practical examples.

Since this is so essential in programming I hope this article has given you some idea of how to implement branching in Swift.

Extend your knowledge

  • Apple have a nice document about statements right (HERE)

The Twitter contact:

Any questions? You can get in touch with me here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store