In this tutorial you will learn about the Swift Operators and its application with practical example.
Swift Operators
An operator is a special symbol that is used to carry out some specific operation on its operand. In Swift, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. Swift operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.
Type of operators in Swift
In Swift, we have following types of operators available –
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
Swift Arithmetic Operators
Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc.
Let variable a holds 20 and variable b holds 10, then −
Operator | Name | Description | Example |
---|---|---|---|
+ |
Addition |
Addition of given operands |
a+b returns 30 |
- |
Subtraction |
Subtraction of second operand from first |
a-b returns 10 |
* |
Multiply |
Multiplication of given operands |
a*b returns 200 |
/ |
Division |
Returns Quotient after division |
a/b returns 2 |
% |
Modulus |
Returns Remainder after division |
a%b returns 0 |
Example:-
1 2 3 4 5 6 7 8 |
var a:Int = 20 var b:Int = 10 print("W3Adda - Swift Arithmetic Operators") print(a+b) print(a-b) print(a*b) print(a/b) print(a%b) |
Output:-
Swift Assignment Operators
Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression.
Operator | Description | Expression |
---|---|---|
= |
Assignment Operator |
a=b |
+= |
add and assign |
a+=b is equivalent to a=a+b |
-= |
subtract and assign |
a-=b is equivalent to a=a-b |
*= |
multiply and assign |
a*=b is equivalent to a=a*b |
/= |
divide and assign |
a/=b is equivalent to a=a/b |
%= |
mod and assign |
a%=b is equivalent to a=a%b |
<<= |
Left shift AND assign |
a<<=5 is equivalent to a=a<<5 |
>>= |
Right shift AND assign |
a>>=5 is equivalent to a=a>>5 |
&= |
Bitwise AND assign |
a&=5 is equivalent to a=a&5 |
^= |
Bitwise exclusive OR and assign |
a^=5 is equivalent to a=a^5 |
|= |
Bitwise inclusive OR and assign |
a|=5 is equivalent to a=a|5 |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var a:Int = 30 var b:Int = 5 print("W3Adda - Swift Assignment Operators") a+=b print("a+=b :\(a)") a-=b print("a-=b :\(a)") a*=b print("a*=b :\(a)") a/=b print("a/=b :\(a)") a%=b print("a%%=b :\(a)") |
Output:-
Swift Comparison (Relational) Operators
Comparison Operators are used evaluate a comparison between two operands. The result of a comparison operation is a Boolean value that can only be true or false. Comparison Operators are also referred as relational operators.
Let variable a holds 20 and variable b holds 10, then −
Operator | Description | Example |
---|---|---|
> |
greater than |
a>b returns TRUE |
< |
Less than |
a<b returns FALSE |
>= |
greater than or equal to |
a>=b returns TRUE |
<= |
less than or equal to |
a<=b returns FALSE |
== |
is equal to |
a==b returns FALSE |
!= |
not equal to |
a!=b returns TRUE |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var a:Int = 20 var b:Int = 10 if a > b { print("W3Adda - Swift Relational Operators") print("a is greater than b.") } else { print("W3Adda - Swift Relational Operators") print("b is greater than a.") } |
Output:-
Swift Logical Operators
Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false.
Let variable a holds true or 1 and variable b holds false or 0, then −
Operator | Name | Description | Example |
---|---|---|---|
&& |
Logical AND |
return true if all expression are true |
(a && b) returns false |
|| |
Logical OR |
return true if any expression is true |
(a || b) returns true |
! |
Logical NOT |
return complement of expression |
!a returns false |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 |
var a:Int = 20 var b:Int = 10 var c:Int = 25 var flag:Bool = false var result:Bool print("W3Adda - Swift Logical Operators") result = (a > b) && (a > c) print("(a>b) && (a>c) : \(result)") result = (a > b) || (a > c) print("(a>b) || (a>c) : \(result)") result = !flag print("!flag : \(result)") |
Output:-
String concatenation Operator
In Swift, strings can be concatenated using the +
operator or the +=
assignment operator.
Swift Ternary Operator ( ? : )
In Swift, ternary operator is considered as short hand for if-else statement.
Syntax:
1 |
condition ? result1 : result2 |
If condition is true the expression will return result1, if it is not it will return result2.
Example:-
1 2 |
let result = (10 > 15) ? "Greater" : "Smaller" print(result) |
Output:-
1 |
Smaller |
Swift Range Operator
In Swift, range operator is used to define range of numbers (x…y) starting from x to y but the value of x always must be less than y. It is generally used to iterate over with loop statements.
Example:-
1 2 3 4 5 |
let studets = ["John", "Keith"] let count = studets.count - 1 for i in 0...count { print("Student \(i + 1) name is \(studets[i])") } |
Ouput:-
1 2 |
Student 1 name is John Student 2 name is Keith |