Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
A swift operator is also a symbol that helps to perform various logical and mathematical computations. Swift also supports most of the standard C operators.
They are classified into three types:
Different operators present in swift are as follows:
Now let us see each type of operator in detail:
These operators are also used to perform mathematical calculations on the operands.
Operator | Symbol | Explanation | Format |
Addition | + | Adds given two operands | x + y |
Subtraction | – | Subtracts right operand from the left one. | x – y |
Multiplication | * | Multiplies two operands | x * y |
Division | / | Divides the numerator by the denominator | x / y |
Modulus | % | Returns the remainder after performing division | x % y |
Example:
print(5 + 2)
print(5 – 2)
print(5 * 2)
print(5 / 2)
print(5 % 2)
Output:
7
3
10
2
1
These operators also return Boolean values taking Boolean values as input.
Operator | Symbol | Explanation | Format |
Logical AND | && | Returns true if all expressions are true else return false | x && y |
Logical OR | || | Returns false if all expressions are false else return true | x || y |
Logical NOT | ! | Inverses the input i.e. return true for false and vice versa | !x |
Example:
print(true && true)
print(true && false)
print(true || false)
print(false || false)
print(! false)
Output:
true
false
true
false
true
These operators are also used to assign values to a variable.
Operator | Symbol | Explanation | Format |
Assignment | = | Also Assigns a value of right operand to the left operand | x = y |
Addition | += | Adds two operands and then assigns a value to left operand | x += y |
Subtraction | -= | Subtracts right operand from left operand and then assigns the value to left operand | x -= y |
Multiplication | *= | Multiplies two operands and then assigns a value to left operand | x *= y |
Division | /= | Divides the numerator by denominator and then assigns a value to left operand | x /= y |
Modulus | %= | Returns remainder after division and then assigns a value to left operand | x %= y |
Bitwise AND | &= | Compares the binary value of two operands, return 1 if both operands are 1 else return 0 and assign a value to the left operand | x &= y |
Bitwise OR | |= | Compares the binary value of two operands, return 0 if both operands are 0 else return 1 and assign the value to the left operand | x |= y |
Bitwise XOR | ^= | Compares the binary value of two operands, return 0 if both operands are same else return 1 and assign a value to the left operand | x ^= y |
Left Shift | <<= | Shifts the bits towards left and assign the result to the left operand | x <<= 2 |
Right Shift | >>= | Shifts the bits towards the right and assign the result to the left operand | x >>= 2 |
let a = 5
print (a)
var x = 6
print(x += 3)
print(x -= 3)
print(x *= 3)
print(x /= 3)
print(x %= 3)
print(x &= 3)
print(x |= 3)
print(x ^= 3)
print(x <<= 2)
print(x >>= 2)
Output:
5
9
3
18
2
0
2
7
5
8
1
These operators help to compare two values and return Boolean values as output.
Operator | Symbol | Explanation | Format |
Equal to | == | Returns true if both operands are equal else return false | x == y |
Not Equal to | != | Returns true if both operands are not equal else return false | x != y |
Greater than | > | Returns true if the left operand is greater than right else return false | x > y |
Less than | < | Returns true if the left operand is smaller than right else return false | x < y |
Greater than or equal to | >= | Returns true if the left operand is greater than or equal to the right else return false | x >= y |
Less than or equal to | <= | Returns true if the left operand is smaller than or equal to the right else return false | x <= y |
print(5 == 2)
print(5 != 2)
print(5 > 2)
print(5 < 2)
print (5 >= 5)
print (5 <= 2)
Output:
false
true
true
true
true
false
Operator | Symbol | Explanation | Format |
Binary AND | & | Check the operands bitwise and return 1 if both bits are 1 else return 0 | x & y |
Binary OR | | | Check the operands bitwise and return 0 if both bits are 0 else return 1 | x | y |
Binary XOR | ^ | Check the operands bitwise and return 0 if both bits are same else return 1 | x ^ y |
Binary NOT | ~ | Returns ones complement i.e. changes 1 to 0 and vice versa | ~x |
Binary Left Shift | << | Bits of the left operand is shifted left side by the number of bits mentioned by the right operand | x << 4 |
Binary Right Shift | >> | Bits of the left operand is shifted right side by the number of bits mentioned by the right operand | x >> 4 |
var a = 8
var b = 7
print(a & b)
print(a | b)
print(a ^ b)
print(~ b)
print(a << 2)
print(a >> 2)
Output:
0
15
15
8
0
2
These operators are used as shortcuts to express the range of values.
Operator | Symbol | Explanation | Format |
Closed Range | (a…b) | It defines a range from a to b, both included | 1…5 |
Half – Open Range | (a..<b) | It defines the range from a to b, a included while b excluded | 1..<5 |
One-sided Range | a.. ..a | It defines the range from a to end of elements or from start to a | 1… …2 |
for i in 1…4 {
print(i)}
for j in 1. . <4 {
print(j) }
let range = ..<4
print(range.contains(2))
Output:
1
2
3
4
1
2
3
true
Operator | Symbol | Explanation | Format |
Unary Plus | + | This toggles the sign of the numeric value to plus | +5 |
Unary Minus | – | This toggles the sign of the numeric value to minus | -6 |
Ternary condition | ? : | Used to check a condition and then give output accordingly | Condition? a : b |
var a = -3
print(+a)
print(-a)
let b = (6==6) ? “True”: “false”
print(b)
Output:
-3
3
Tru
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.