Swift - Logical Operators



Logical Operator in Swift

Logical operators are used to perform logical operations on multiple expressions together. They always return boolean values either true or false. They are commonly used with conditional statements and loops to return decisions according to the boolean conditions.

You can also combine them to manipulate boolean values while dealing with complex expressions. Swift supports the following logical operators −

Operator Name Example
&& AND A && B = false
|| OR A || B = true
! NOT !(A && B) = true

AND Operator in Swift

The AND operator returns true if both the given expressions are true. If any one of the expressions is false, then this operator will return false. It is an infix operator means this operator only works when you place it in between two operands.

Syntax

Following is the syntax of the AND operator −

var result = expression1 && expression2

Example

Swift program to perform logical AND operation between two expressions using AND operator (&&).

import Foundation

let age = 22
let height = 185

if (age > 18) && (height > 182){
   print("You are eligible for the Police exam")
} else {
   print("You are not eligible")
}

Output

You are eligible for the Police exam

OR Operator in Swift

The OR operator returns true if only one of the expressions among the two given expressions is true. If both the expressions are false, then this operator will return false. It is an infix operator means it will be placed between two operands to perform its action.

Syntax

Following is the syntax of the OR operator −

var result = expression1 || expression2

Example

Swift program to perform logical OR operation between two expressions using OR operator (||).

import Foundation
let marks = 640
let experience = 3
if (marks > 500) || (experience > 4){
   print("You are eligible for the PO Post")
} else {
   print("You are not eligible")
}

Output

You are eligible for the PO Post

NOT Operator in Swift

A NOT operator is used to invert the boolean value. If the value is true, then it will convert into false. Whereas if the value is false then it will convert into true. It is a prefix operator and is placed just before the expression whose value you want to invert.

Syntax

Following is the syntax of the NOT operator −

!(expression1 && expression2)

Example

Swift program to perform logical NOT operation on the expressions using NOT operator (!).

import Foundation
let x = 20
let y = 40

// Here expression gives a true result but NOT operator converts it into a false
if !(x > 0 && y > 0) {
   print("Given values are greater than 0")
} else {
   print("Given values are less than 0")
}

Output

Given values are less than 0

Combining Logical Operators in Swift

In Swift, we can also combine multiple logical operators in a single expression. It will create a long compound expression. As we know AND and OR are left-associative so in the compound expression the left-side expression will evaluate first.

Example

Swift program to combine multiple logical operators.

import Foundation

let password = "vsv@v3"
let username = "mona"
let mainPassword = "MO@12s"

if (password == "XP123" || username == "mona") && mainPassword == "MO@12s"{
   print("Welcome to the digital locker")
} else {
   print("Error!!! Please enter correct detail")
}

Output

Welcome to the digital locker
swift_operators.htm
Advertisements