Swift Operators

An operator is a symbol that specifies an action to perform in an expression, for example, the ‘+’ symbol in the expression ‘1 + 2’ would add the two numbers together to produce a result. This type of operator is known as an arithmetic operator, however, there are also other groups of operators within Swift that will be explained below.

Arithmetic Operators

Arithmetic operators allow for mathematical calculations to be carried out within a Swift script.

Operator Description
+ Adds two values together.
Subtracts one value from another.
* Multiplies two values together.
/ Divides one value by another.
% Calculates the remainder after one value is divided by another.

The following examples show each of these operators in action.

var a = 10
var b = 20

print(a + b)
print(b - a)
print(a * b)
print(b / a)
print(b % a)

The output from this is as follows.

30
10
200
2
0

Assignment Operators

Assignment operators in C# are used to assign values to a variable, the most basic of which is ‘=’. Here, the ‘=’ symbol means ‘gets set to’ rather than ‘equals’, so in the expression ‘x = y’, ‘x’ gets set to ‘y’. The arithmetic operator symbols can be combined with ‘=’ to form the assignment operators.

Operator Description
= Assigns the value of the right side operand to the left side operand.
+= Adds the value from the right side operand to the left side operand and assigns the value to the left side operand.
-= Subtracts the value of the right side operand from the left side operand and assigns the value to the left side operand.
*= Multiplies the value from the right side operand with the left side operand and assigns the value to the left side operand.
/= Divides the left side operand by the value of the right side operand and assigns the value to the left side operand.
%= Takes the resulting modulus of the operands on both sides and assigns it to the left hand operand.

The examples below show these assignment operators in action.

var a = 10 // a gets set to 10.

var b = 2
b += 5 // b gets set to 7.

var c = 10
c -= 4 // c gets set to 6.

var d = 2
d *= 2 // d gets set to 4.

var e = 20
e /= 5 // e gets set to 4.

var f = 5
f %= 2 // f gets set to 1.

Relational Operators

Relational operators compare two values and are used, for example, in ‘if’ statements, which are described in the section on Decision Making. The result of the comparison is either True or False.

Operator Description
== Evaluates to True if the two values are equal.
!= Evaluates to True if the two values are not equal.
> Evaluates to True if the first value is greater than the second.
< Evaluates to True if the first value is less than the second.
>= Evaluates to True if the first value is greater than or equal to the second.
<= Evaluates to True if the first value is less than or equal to the second.

Logical Operators

Logical operators are used where you want to test more than one condition at the same time, for example, in an ‘if’ statement, which is discussed in the section on Decision Making.

Operator Description
&& Logical AND operator where operands on both sides must be True to evaluate to True.
|| Logical OR operator where only one of the two operands must be True to evaluate to True.
! Logical NOT operator that can be used in conjunction with ‘&&’ or ‘||’ to reverse the resulting operand. In the case of ‘&&’, if both conditions evaluate to True, then with the use of the ‘!’ operator the result would be False.