Swift – Operators

02 Feb, 2022
Swift is a general-purpose, multi-paradigm, and compiled programming language which is developed by Apple Inc. In Swift, an operator is a symbol that tells the compiler to perform the manipulations in the given expression. Just like other programming languages, the working of operators in Swift is also the same. We can perform multiple operations using operators in our programs there are multiple operators available in Swift. In this article, we will learn all types of operators which are present in the Swift programming language.
+ (Addition): This operator is used to add two variables.
– (Subtraction): This operator is used to subtract the second variable from the first.
/ (Division): This operator is used to divide the first variable from the second.
* (Multiplication): This operator is used to multiply the first variable with the second.
% (Modulus): This operator gives the remainder after division first from second.
Swift
// Swift program to illustrate the // use of arithmetic changesimport Swift  // Creating two variables a and bvar a = 5var b = 4  // Adding two numbervar sum = a + bprint("Addition:", sum)  // Subtracting two numbervar sub = a - bprint("Subtraction:", sub)  // Dividing two numbervar div = a / bprint("Divide:", div)  // Multiplying two numbervar mul = a * bprint("Multiply:", mul)  // Finding Modulusvar mod = a % bprint("Modulus:", mod)
Output:
Addition: 9
Subtraction: 1
Divide: 1
Multiply: 20
Modulus: 1
= (Equal): This operator is used to assign values from the right side variable to the left side variable.
+= (Plus equal to): This operator is used to add the right variable to the left variable and assign the result to the left variable.
-= (Minus equal to): This operator is used to subtract the right variable from the left variable and assign the result to the left variable.
/= (Divide equal to): This operator is used to divide the left variable with the left variable and assigns the result to the left variable.
*= (Multiply equal to): This operator is used to multiply the right variable with the left variable and assign the result to the left variable.
%= (Modulus equal to): This operator is used to take modulus using two variables and assigns the result to the left variable
Swift
// Swift program to illustrate the // use of assignment operatorsimport Swift  // Creating two variables a & bvar a = 5var b = 1  // Plus equal toa += bprint("Result of += operator: ", a)  // Minus equal toa -= bprint("Result of -= operator: ", a)  // Divide equal toa /= bprint("Result of /= operator: ", a)  // Multiply equal toa *= bprint("Result of *= operator: ", a)  // Modulus equal toa %= bprint("Result of %= operator: ", a)
Output:
6
5
5
5
0
<< (Left Shift): The left variable value is moved left by the number of bits mentioned by the right variable.
>> (Right Shift): The left variable value is moved right by the number of bits mentioned by the right variable.
& (Binary AND): This operator copies bits to the result if the bits are set in both variables.
| (Binary OR): This operator copies bits to the result if the bits are set in any variables.
^ (Binary XOR): This operator copies bits to the result if the bits are set on only one variable.
~ (Tilde): This operator gives binary one’s complement of the variable
Swift
// Swift program to illustrate the // use of bitwise operatorsimport Swift  // Creating variables a and bvar a = 2var b = 1  // Left shift operatorprint("Bitwise left shift operator: ", a << b)  // Right shift operatorprint("Bitwise right shift operator: ", a >> b)  // AND operator operatorprint("AND operator: ", a & b)  // OR operatorprint("OR operator: ", a | b)  // XOR operatorprint("XOR operator: ", a ^ b)  // Tilde operatorprint("Tilde operator: ", ~a)
Output:
4
1
0
3
3
-3
< (Less than): If the value of the left variable is less than the value of the right variable then the condition becomes true.
> (Greater than): If the value of the left variable is greater than the value of the right variable then the condition becomes true.
== (Equal to equal to): If the value of both variables is equal then the condition becomes true.
<= (Less than Equal to): If the value of the left variable is less than or equal to the value of the right variable then the condition becomes true.
>= (Greater than Equal to): If the value of the left variable is greater than or equal to the value of the right variable then the condition becomes true.
!= (Not equal to): If the value of both variables is not equal then the condition becomes true.
Swift
// Swift program to illustrate the // use of Comparison operatorsimport Swift  // Creating two variables a & bvar a = 2var b = 1  // Less than operatorprint(a < b)  // Greater than operatorprint(a > b)  // Equal to operatorprint(a == b)  // Not equal to operatorprint(a != b)  // Less than equal to operatorprint(a <= b)  // Greater than equal to operatorprint(a >= b)
Output:
false
true
false
true
false
true
&& (Logical AND): If both the side conditions are true, then the condition becomes true.
|| (Logical OR): If any side condition is true, then the condition becomes true.
! (Logical NOT): This operator is to reverse the logical state.
Swift
// Swift program to illustrate the // use of Logical Operatorsimport Swift  // Creating variables a, b & cvar a = 1var b = 2var c = 3  // Logical AND operatorif (a < b && b < c){    print("Both condition is true")}  // Logical OR operatorif (a < b || a > c){    print("Only one condition is true")}  // Logical NOT operatorif (true && !false){    print("Logic reversed")}
Output:
Both condition is true
Only one condition is true
Logic reversed
Cond ? A : B (Ternary Operator): If the condition becomes true, then value A will be assigned otherwise the value B will be assigned.
(A ?? B) (Nil-coalescing Operator): If the user didn’t pass any value to the variable then the default value will be nil.
++ (Increment): This operator has been used to increase the value of the variable by 1.
— (Decrement): This operator is used to decrease the value of the variable by 1.
Swift
// Swift program to illustrate the use of// Ternary and Nil-coalescing Operator import Swift  // Creating variables a, bvar a = 1var b = 2  // Ternary operatorvar c = a < b ? 3 : 4print(c)  let name: String? = nil  // Nil-Coalescing Operatorlet newName = name ?? "GFG"  // If we don't pass GFG then the// default value will be nil in newNameprint(newName)
Output:
3
GFG
(a...b) (Closed Range Operator) — It runs from a to b including the values a and b. It is used in mostly used in for-in loops.
(a..<b) (Half-Open Range Operator) — It runs from a to b, but it doesn’t include the value of b. It is used in mostly used in for-in loops.
[a...] (One-Sided Range Operator) — It will run for ranges that continue as far as possible in one direction.
Swift
// Swift program to illustrate the use of// range Operatorsimport Swift  // Closed Range Operator - It will print up to 3for i in 1...3 {    print("closed = \(i)")}  // Half open Range Operator - It will only print up to 2for i in 1..<3 {    print("half = \(i)")}  // One sided Range Operator - It will print all// values in array from index 0let values = [ 1, 2, 3, 4 ]for i in values[0...] {    print("One sided = \(i)")}
Output:
closed = 1
closed = 2
closed = 3
half = 1
half = 2
One sided = 1
One sided = 2
One sided = 3
One sided = 4
In swift operator, precedence is used to find the grouping terms in the given expression. Also, used to evaluate the expression. For example, y = 3 + 4 * 2, here * has the highest precedence so first, we solve 4 *2 and then we add the result with 3. Precedence will decrease while we visit the bottom of the table given below.
sagartomar9927
Picked
Swift-Basics
Swift
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.