Operators
An operator is a symbol or combination of symbols that we can use to check, change, or combine values. We have used operators in most of the examples so far in this book , but we did not specifically call them operators. In this section, we will show how to use most of the basic operators that Swift supports.
Swift supports most standard C operators and also improves them to eliminate several common coding errors. For example, the assignment operator does not return a value, which prevents it from being used where we meant to use the equality operator (==
).
Let's look at the operators in Swift.
The assignment operator
The assignment operator initializes or updates a variable.
Prototype:
varA = varB
Example:
let x = 1 var y = "Hello" a = b
Comparison operators
The comparison operator returns a Boolean true
if the statement is true or a Boolean false
if the statement is not true.
Prototypes:
Equality: varA == varB Not equal: varA != varB Greater than: varA > varB Less than: varA < varB Greater than or equal to: varA >= varB Less than or equal to: varA <= varB
Example:
2 == 1 //false, 2 does not equal 1 2 != 1 //true, 2 does not equal 1 2 > 1 //true, 2 is greater than 1 2 < 1 //false, 2 is not less than 1 2 >= 1 //true, 2 is greater or equal to 1 2 <= 1 //false, 2 is not less or equal to 1
Arithmetic operators
The arithmetic operators perform the four basic mathematical operations.
Prototypes:
Addition: varA + varB Subtraction: varA - varB Multiplication: varA * varB Division: varA / varB
Example:
var x = 4 + 2 //x will equal 6 var x = 4 - 2 //x will equal 2 var x = 4 * 2 //x will equal 8 var x = 4 / 2 //x will equal 2 var x = "Hello " + "world" //x will equal "Hello World"
The remainder operator
The remainder operator calculates the remainder if the first operand is divided by the second operand.
Prototype:
varA % varB
Example:
var x = 10 % 3 //x will equal 1 var x = 10 % 2.6 //x will equal 2.2
Compound assignment operators
The compound assignment operators combine an arithmetic operator with an assignment operator.
Prototypes:
varA += varB varA -= varB varA *= varB varA /= varB
Example:
var x = 6 x += 2 //x is equal to 8 x -= 2 //x is equal to 4 x *= 2 //x is equal to 12 x /= 2 //x is equal to 3
The ternary conditional operator
The ternary conditional operator assigns a value to a variable based on the evaluation of a comparison operator or Boolean value.
Prototype:
(boolValue ? valueA : valueB)
Example:
var x = 2 var y = 3 var z = (y > x ? "Y is greater" : "X is greater") //z equals "Y is greater"
The logical NOT operator
The logical NOT operator inverts a Bboolean value.
Prototype:
varA = !varB
Example:
var x = true var y = !x //y equals false
The logical AND operator
The logical AND operator returns true
if both operands are true; otherwise it returns false
.
Prototype:
varA && varB
Example:
var x = true var y = false var z = x && y //z equals false
The logical OR operator
The logical OR operator returns true
if either of the operands is true.
Prototype:
varA || varB
Example:
var x = true var y = false var z = x || y //z equals true
For those who are familiar with the C language or similar languages, these operators should look pretty familiar. For those who aren't that familiar with the C operators, rest assured that you will use them enough and they will become second nature.