Logical Operators in Swift

|| && !

Image for post
Image for post
Photo by Michael Dziedzic on Unsplash

Logical operators give you the ability to control program flow. They are so important that it is essential that we think about how these work in Swift.

This guide is here to help you, and give the guidance you need!

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)

WITH

  • To understand the truth tables included below some basic knowledge of Binary for programmers would be useful (guide HERE)
  • Some understanding of the meaning of business logic (guide HERE)

Terminology

Boolean: a data type is any data type that has either a true or false value, yes or no value, or on or off (1 or 0) value

Logical Operators: symbols that allow a program to make a decision based upon multiple condition

Truth Table: a mathematical table used in logic

Logical Operators

The general logical operators are defined in the truth tables below which show the logical operators and the result expressed as a boolean.

Tradition dictates that the first element of the expression is called p, and the second (where there is a second element) is called q.

Image for post
Image for post

Now this can be broken down into truth tables — and I’ve added VENN diagrams to make this a little easier for you too!

AND

When both of our inputs, p and q, are true then the result (p ^ q)is true. All other results are false.

Image for post
Image for post
Image for post
Image for post

OR

When either of our inputs, p and q, are true then the result (p ∨ q) is true. All other results (where both inputs are false) are false.

Image for post
Image for post
Image for post
Image for post

EXCLUSIVE OR

When either of our inputs, p and q, are true then the result (p ∨ q) is true with the exeception that this is exclusive of when both the inputs are true. All other results (where both inputs are false) are false.

Image for post
Image for post
Image for post
Image for post

NOT

Our input, p is reversed. That is, if it was true it becomes false and if false it becomes true.

Image for post
Image for post
Image for post
Image for post

So how can these be implemented in Swift? It turns out: It’s not too difficult.

The Swift implementation

The logical AND

And is solved in Swift with &&.

The following statements are written followed by a comment with the expected result in Swift

true && false // false
false && true // false
true && true //true
false && false // false

This is often used in an IF statement

if (male && lazy) {
print ("You are unemployed")
}

The Logical OR

If either the first element or second element are true, the statement is true. This is the same in Swift.

true || false // false
false || true // false
true || true // true
false || false // false

so an example of this is the following:

if (sleepy || asleep) {
print ("You should be in bed")
}

The Exclusive OR

Exclusive OR is the same as not equals, which can be calculated in Swift by

true != false // true
false != true // true
true != true // false
false != false // false

Which gives us a potential example

if (!asleep) {
print ("You are awake (or dead)")
}

The Logical NOT

A boolean can either be true or false. Not before that boolean makes the boolean result the opposite of the value it was before. In other words Swift processes this like the following

!true // false
!false // true

so we have an example in Swift of that:

if (!preventPrinting) {
print ("This is going to print")
}

Mixing Logical Operators in Swift

There are so many combinations that can be used, depending on the business logic of your application.

Here is an example of mixing a couple of them (note the use of brackets):

if (!inNewYork && ( inLA || inSF ) ) {
print ("In LA of San Francisco")
}

Conclusion:

Logical operators are a fundamental building block of programming. As a result you need to make sure that you are not only familiar with them, but are able to put them into practice according to the business logic that will make your particular app a fantastic success (hopefully!).

Extend your knowledge

  • Apple have a nice document about basic operators (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