4 Basic Operators
WHAT YOU WILL LEARN IN THIS CHAPTER:
- How to assign values to variables and constants using the assignment operator
- How to perform arithmetic operations using the arithmetic operators
- How to increment and decrement variables by one using the increment and decrement operators
- Using increment and decrement variables using the compound assignment operators
- How to use the nil coalescing operator when dealing with optional types
- How to perform comparisons using the comparison operators
- How to specify ranges using the range operator
- How to perform logical comparisons using the logical operators
In the previous two chapters you learned about the various data types in Swift and how Swift deals with strings and characters. In this chapter, you will learn about the various operators available in Swift. Operators work with the various data types to enable you to make logical decisions, perform arithmetical calculations, as well as change values. Swift supports the following types of operators:
- Assignment
- Arithmetic
- Comparison
- Range
- Logical
ASSIGNMENT OPERATOR
The assignment operator (=
) sets a variable or constant to a value. In Swift, you can create a constant by assigning a value to a constant name as shown here:
let companyName = "Developer Learning Solutions"
let factor = 5
You can also create a variable using the assignment operator:
var customerName1 = "Richard"
Besides assigning a value to a variable or constant, you can also assign a variable ...
Get Beginning Swift Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.