Skip to Content

Toán tử logic (Logical Operators) trong Swift

1. AND Operator (&&)

let age = 25 let hasLicense = true if age >= 18 && hasLicense { print("Can drive") }

2. OR Operator (||)

let isWeekend = true let isHoliday = false if isWeekend || isHoliday { print("Day off!") }

3. NOT Operator (!)

let isRaining = false if !isRaining { print("Go outside") }

4. Combining Operators

let age = 25 let hasLicense = true let hasInsurance = false if age >= 18 && hasLicense && !hasInsurance { print("Need insurance") }

5. Short-circuit Evaluation

let result1 = false && expensiveCheck() // Không gọi expensiveCheck() let result2 = true || expensiveCheck() // Không gọi expensiveCheck()

📝 Tóm tắt

  • && - AND (cả hai true)
  • || - OR (một trong hai true)
  • ! - NOT (đảo ngược)
  • Short-circuit evaluation
  • Thứ tự: ! > && > ||
Last updated on