Understanding Assignment Operators
Introduction to Assignment Operators
Assignment operators in Swift are special symbols used to assign or
update values of variables or constants. These operators ensure that
the right-hand side (value) is assigned to the left-hand side
(variable or constant). The data type on both sides must match;
otherwise, an error occurs. Assignment operators in Swift follow
right-to-left associativity.
Swift supports two main types of assignment operators:
1. Simple Assignment Operator: Assigns a value to a variable.
2. Compound Assignment Operators: Combines the assignment
operator (=) with arithmetic or bitwise operations.
List of Assignment Operators in Swift
Name | Example |
---|---|
Assignment | var x = 10 |
Add and Assignment | A += B → A = A + B |
Subtract and Assignment | A -= B → A = A - B |
Multiply and Assignment | A *= B → A = A * B |
Divide and Assignment | A /= B → A = A / B |
Modulo and Assignment | A %= B → A = A % B |
Bitwise AND and Assignment | A &= B → A = A & B |
Bitwise Inclusive OR and Assignment | A |= B → A = A | B |
Bitwise Exclusive OR and Assignment | A ^= B → A = A ^ B |
Left Shift and Assignment | A <<= B → A = A << B |
Right Shift and Assignment | A >>= B → A = A >> B |
Simple Assignment Operator (=)
The = operator assigns a value to a variable or constant.
Syntax
var number 10
Example
import
Foundation
let myString:
String =
"SwiftProgramming"
print("String =", myString)
Output
Add and Assignment Operator (+=)
The += operator adds the right-hand value to the left-hand value and assigns the result to the left-hand variable.
Syntax
X +=Y
Example
import
Foundation
var num1
= 15
var num2
= 25
num1 +=
num2
print("Sum =",
num1)
Output
Subtract and Assignment Operator (-=)
The -= operator subtracts the right-hand value from the left-hand value and assigns the result to the left-hand variable.
import
Foundation
var num1
= 50
var num2
= 30
num1 -=
num2
print("Result =", num1)
Output
Multiply and Assignment Operator (*=)
The *= operator multiplies the left operand by the right operand and assigns the result to the left operand.
Example
import
Foundation
var num1
= 6
var num2
= 7
num1 *=
num2
print("Result =", num1)
Output
Divide and Assignment Operator (/=)
The /= operator divides the left operand by the right operand and assigns the result to the left operand.
Example
import
Foundation
var num1
= 100
var num2
= 4
num1 /=
num2
print("Result =", num1)
Output
Modulo and Assignment Operator (%=)
The %= operator calculates the remainder when dividing the left operand by the right operand and assigns the result to the left operand.
Example
import
Foundation
var num1
= 29
var num2
= 4
num1 %=
num2
print("Result =", num1)
Output
Bitwise AND and Assignment Operator (&=)
The &= operator performs a bitwise AND operation and assigns the result to the left operand.
Example
import
Foundation
var num1:
UInt8 =
0b10101010
let num2:
UInt8 =
0b11001100
num1 &=
num2
print("Result =", String(num1,
radix: 2))
Output
Bitwise Inclusive OR and Assignment Operator (|=)
The |= operator performs a bitwise OR operation and assigns the result to the left operand.
Example
import
Foundation
var num1:
UInt8 =
0b10101010
let num2:
UInt8 =
0b11001100
num1 |=
num2
print("Result =", String(num1,
radix: 2))
Output
Left Shift and Assignment Operator (<<=)
The <<= operator shifts bits to the left and assigns the result to the left operand.
Example
import
Foundation
var number:
UInt8 =
0b00010101
number
<<= 2
print("Result =", String(number, radix: 2))
Output
Right Shift and Assignment Operator (>>=)
The >>= operator shifts bits to the right and assigns the result to the left operand.
Example
import
Foundation
var number:
UInt8 =
0b10101000
number >>=
3
print("Result =", String(number, radix: 2))