# Swift Operators Swift Operator: In this tutorial, you’ll learn everything about various types of operators in the Swift programming language, their syntax, and how to use them with examples.

Contents

## What are the Operators in Swift?

Operators are special symbols or phrases that programmers use to check, join, or change values. For instance, the multiplication operator (*) multiplies two numbers together. Another complex example incorporates the logical AND operator && (as in if username && password) and the increment operator ++i, which is a shortcut to increase the value of I by 1. Swift supports most standard C operators and eliminates several common coding errors.

Operators are special symbols that perform operations on variables and values. For instance,

`print(5 + 6)   // 11`

Here, + is an operator that adds two numbers: 5 and 6.

## Types of Operators

Here’s a list of various sorts of Swift operators that you will learn in this tutorial.

1. Arithmetic operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators

## 1. Swift Arithmetic Operator

Arithmetica operator is used to performing mathematical operations like addition, subtraction, multiplication, and so forth For instance,

`var sub = 10 - 5 // 5`

Here, – is an arithmetic operator that subtracts two values or variables.

## Example 1: Arithmetic Operators in Swift

```var a = 7
var b = 2

print (a + b)

// subtraction
print (a - b)

// multiplication
print (a * b)  ```

Output

```9
5
14```

In the above example, we have used

• + to add a and b
• – to subtract b from a
• * to multiply a and b

### / Division Operator

The / operator performs division between two numbers. In any case, if two numbers are integers, we will just get the quotient. For instance,

```// returns quotient only
7 / 2 = 3```

On the off chance that we use the / operator with floating-point numbers, we will get the actual outcome. For instance,

```// perform division
7.0 / 3.0 = 3.5```

### % Modulo Operator

The modulo operator % computes the remainder. For instance,

`print(9 % 4)     // 1`

Note: The % operator can only be used with integers.

## 2. Swift Assignment Operators

Assignment operators are used to assign values to variables. For instance,

```// assign 5 to x
var x = 5```

Here, = is an assignment operator that assigns 5 to x. Here’s a list of various assignment operators accessible in Swift.

## Example 2: Assignment Operators

```// assign 10 to a
var a = 10

// assign 5 to b
var b = 5

// assign the sum of a and b to a
a += b      // a = a + b

print(a)```

Output

`15`

## 3. Swift Comparison Operators

Comparison operators compare two values/variables and return a boolean outcome: true or false. For instance,

```var a = 5, b =2
print (a > b)      // true```

Here, the > comparison operator is used to compare whether a is greater than b or not.

### Example 3: Comparison Operators

```var a = 5, b = 2

// equal to operator
print(a == b)

// not equal to operator
print(a != b)

// greater than operator
print(a > b)

// less than operator
print(a < b)

// greater than or equal to operator
print(a >= b)

// less than or equal to operator
print(a <= b)```

Output

```false
true
true
false
true
false```

Note: Comparison operators are used in decision-making and loops. We’ll discuss more of the comparison operator in later tutorials.

## 4. Swift Logical Operator

Logical operators are used to checking whether an expression is true or false. They are used in decision-making. For instance,

```var a = 5, b = 6
print((a > 2) && (b >= 6))    // true```

Here, && is the logical operator AND. Since both a > 2 and b >= 6 are true, the result is true.

### Example 4: Logical Operators

```// logical AND
print(true && true)      // true
print(true && false)     // false

// logical OR
print(true || false)      // true

// logical NOT
print(!true)                  // false```

Output

```true
false
true
false```

## 5. Swift Bitwise Operators

In Swift, bitwise operators are used to perform operations on individual pieces.

Bitwise operators are rarely used in everyday programming.

## 6. Other Swift Operators

Bitwise administrators are seldom utilized in ordinary programming.

Here’s a list of different operators accessible in Swift. We’ll learn more about these operators in later tutorials

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us. ### HTML RGB and RGBA Colors ### HTML HEX Colors  