Logo
Swift Basics
Swift BasicsOperators

Operators

Operators are the building blocks of any programming language, allowing you to perform various operations on data. This section will introduce you to the different types of operators in Swift, including arithmetic, comparison, and logical operators.

Arithmetic Operators

Basic Arithmetic

Swift supports basic arithmetic operators like addition (+), subtraction (-), multiplication (*), and division (/).

let sum = 5 + 3  // 8
let difference = 9 - 4  // 5

Modulo Operator

The modulo operator (%) gives the remainder of a division operation.

let remainder = 10 % 3  // 1

Comparison Operators

Swift provides operators for comparing values, such as equals (==), not equals (!=), greater than (>), and less than (<).

if age == 30 {
    print("You are 30 years old.")
}

Logical Operators

Logical operators like AND (&&), OR (||), and NOT (!) are used to combine multiple conditions.

if age > 18 && isCompleted {
    print("You are eligible.")
}

Conclusion

Operators in Swift are versatile and powerful, allowing you to manipulate and compare data effectively. Understanding how to use these operators is essential for writing efficient Swift code.

Book a conversation with us for personalize training today!

Was this helpful?
Logo