Swift Logical Operators

In Swift, logical operators are used to combine or invert Boolean values. They play a critical role in decision-making and controlling the flow of programs. Logical operators work with true and false values to form more complex conditions.


Table – Logical Operators in Swift

Here is a list of logical operators available in Swift:

OperatorDescriptionExample
&&Logical ANDReturns true if both operands are true
||Logical ORReturns true if at least one operand is true
!Logical NOTInverts the Boolean value
Logical Operators in Swift

Example: Using Logical Operators

This example demonstrates how each logical operator works:

File: main.swift

</>
Copy
let isSunny = true
let isWarm = false

// Logical AND
print("Is it sunny and warm? \(isSunny && isWarm)") // false

// Logical OR
print("Is it sunny or warm? \(isSunny || isWarm)") // true

// Logical NOT
print("Is it not sunny? \(!isSunny)") // false

Explanation:

  • The && operator checks if both isSunny and isWarm are true. Since isWarm is false, the result is false.
  • The || operator checks if either isSunny or isWarm is true. Since isSunny is true, the result is true.
  • The ! operator inverts the value of isSunny. Since isSunny is true, the result is false.

Output:

Output for Swift Example Program using Logical Operators

Example 1: Combining Conditions

This example shows how logical operators can combine multiple conditions:

File: main.swift

</>
Copy
let age = 20
let hasID = true

if age >= 18 && hasID {
    print("You are allowed to enter.")
} else {
    print("You are not allowed to enter.")
}

Explanation:

  • The condition age >= 18 && hasID checks if the person is at least 18 years old and has an ID.
  • Since both conditions are true, the program prints “You are allowed to enter.”

Output:

Output for Swift Example Program for using Logical AND Operator

Example 2: Using Logical OR

This example demonstrates the use of the || operator:

File: main.swift

</>
Copy
let hasKey = false
let knowsPassword = true

if hasKey || knowsPassword {
    print("You can unlock the door.")
} else {
    print("Access denied.")
}

Explanation:

  • The condition hasKey || knowsPassword checks if the person either has the key or knows the password.
  • Since knowsPassword is true, the program prints “You can unlock the door.”

Output:

Output for Swift Example Program for using Logical OR Operator

Example 3: Using Logical NOT

This example demonstrates the ! operator:

File: main.swift

let isOnline = false

if !isOnline {
    print("The user is offline.")
} else {
    print("The user is online.")
}

Explanation:

  • The ! operator inverts the value of isOnline.
  • Since isOnline is false, !isOnline evaluates to true, and the program prints “The user is offline.”

Output:

Output for Swift Example Program for using Logical NOT Operator

Conclusion

Logical operators are essential tools for combining and manipulating Boolean values in Swift. They are widely used in conditional statements and loops to implement complex logic.