(Swift for Beginners)
Swift Operators
In this article, you’ll learn everything about different types of operators in Swift programming language, their syntax and how to use them with examples.
Operators are special symbols (characters) that carry out operations on operands (variables and values). Some basic operations includes assigning, changing, combining and checking values.
For example, +
is an operator that performs addition.
In Swift variables & constants article, you learned about variables/constants. Now, in this article you will the use of operators on them.
Types of operators
You can categorize operators broadly in two basic categories based on the:
- Number of of operands
- Operation of an operator
According to the number of operands an operator operates on, operators can be categorized as:
1. Unary operator
This operators operate on a single operand.
Example 1: Unary Operator
print(!true)
var a = -5
print(-a)
When you run the above program, the output will be:
false 5
2. Binary operator
This operator operates on a two operands.
Example 2: Binary Operator
let result = 20 + 30
print(result)
When you run the above program, the output will be:
50
3. Ternary Operators
This operator operates on three operands. Visit Swift Ternary Conditional Operator to learn more about it.
Example 3: Ternary Operator
let result = (5 > 10) ? "Value larger" : "Value Smaller"
print(result)
When you run the above program, the output will be:
Value Smaller
According to the operation a operator does, it can be categorized as:
1. Assignment operators
Assignment operator are used in swift to assign values to a property (variable/constant).
Operator | Meaning |
---|---|
= | Simple assignment operator, Assigns values from right side operands to left side operand |
+= | Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand |
-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand |
*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand |
/= | Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand |
%= | Modulus AND assignment operator, It takes modulus using two operands and assigns the result to left operand |
<<= | Left shift AND assignment operator |
>>= | Right shift AND assignment operator |
&= | Bitwise AND assignment operator |
^= | bitwise exclusive OR and assignment operator |
|= | bitwise inclusive OR and assignment operator |
Example 4: Normal Assignment Operator
let age = 10
let platform = "iOS"
print(age)
print(platform)
When you run the program, the output will be:
10 iOS
The above example assigns the integer value 10 to the constant age. So the statement print(age)
outputs 10 in the console.
Likewise, the statement let platform = "iOS"
assigns the string literal "iOS"
to the constant platform. Therefore, the statement print(platform)
outputs iOS in the console.
Example 5: Compound Assignment Operator
var x = 10
x -= 2
print(x)
When you run the program, the output will be:
8
The expression x -= 2
uses a compound assignment operator (-=)
and is a shorthand for x = x - 2
. The operator is a compound assignment operator because the operator performs both task subtraction and assignment at the same time.
You can find examples on bitwise operators on this article Swift Bitwise operators.
2. Arithmetic operators
These operators are used to perform mathematical operations that includes multiplication, division, addition and subtraction etc. This operators fall into category of binary operator that takes two operands.
Operator | Meaning |
---|---|
+ | Addition (also used for string concatenation) |
– | Subtraction Operator |
* | Multiplication Operator |
/ | Division Operator |
% | Remainder Operator |
Example 6: Simple Arithmetic Operations
print(10 + 20)
print(10 - 20)
print(2 * 5)
print(5 / 2 ) //division operator
print(5 % 2 ) //remainder operator
print("I love " + "Swift") //operator can also be used to concatenate string
When you run the program, the output will be:
30 -10 10 2 1 I love Swift
Example 7: Arithmetic Operator
You may use the result to store into a variable or constant using assignment operators as
let x = 10 / 5
let y = 3 % 2
print(x)
print(y)
When you run the program, the output will be:
2 1
3. Comparison Operators
These operators allow you to compare two values. Each of the comparison operators returns a Bool value to indicate whether or not the statement is true. Swift supports following types of comparison operators:
Operator | Meaning | Example |
---|---|---|
== | equal to | 5 == 3 is evaluated to false |
!= | not equal to | 5 != 3 is evaluated to true |
> | greater than | 5 > 3 is evaluated to true |
< | less than | 5 < 3 is evaluated to false |
>= | greater than or equal to | 5 >= 5 is evaluated to true |
<= | less than or equal to | 4 <= 5 is evaluated to true |
Example 8: Comparison Operator
let msg = "Hello"
print(msg == "Hello")
print(msg != "Hello")
When you run the program, the output will be:
true false
Example 9: Greater than and less than comparison operators
print(10 > 20)
print(10 < 20)
print(5 >= 5)
print(5 <= 4)
When you run the program, the output will be:
false true true false
4. Logical Operators
These operators are used with Boolean (logical) values and returns a Boolean value. It is mainly used to control program flow with if else, while, or some other control statement.
Operator | Meaning | Example |
---|---|---|
|| | Logical-OR; true if either of the boolean expression is true | false || true is evaluated to true |
&& | Logical-AND; true if all boolean expressions are true | false && true is evaluated to false |
Example 10: Logical Operator
print(true && true)
print(true && false)
print(false || true)
When you run the program, the output will be:
true false true
This article explains some of the basic operators in Swift. However, there are few more advanced operators like Range Operators, Nil-Coalescing Operator in Swift which you’ll learn in the coming tutorials.
Next, you’ll learn about precedence and associativity of Swift operators. Simply said, it is the order of execution of these operations in an expression.
Swift programming for Beginners – Swift Operators
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.