# Logical Operators in Swift

## || && !

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.

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.

## 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.

## 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.

## NOT

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

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 // falsefalse && true // falsetrue && true //truefalse && 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 // falsefalse || true // falsetrue || true // truefalse || 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 // truefalse != true // truetrue != true // falsefalse != 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!).

• Apple have a nice document about basic operators (HERE)

Any questions? You can get in touch with me here

Written by