- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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