Exploring operators
You can perform arithmetic, comparison, and logical operations in Swift. Arithmetic operators are for common mathematical operations. Comparison and logical operators check an expression's value and return true
or false
.
For more information on operators, visit https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html.
Let's look at each operator type in more detail. You'll start with arithmetic operators (addition, subtraction, multiplication, and division) in the next section.
Using arithmetic operators
You can perform mathematical operations on integer and floating-point numbers by using the standard arithmetic operators shown here:
Let's see how these operators are used. Follow these steps:
- Add the following code to add arithmetic operations to your playground:
let sum = 23 + 20
let result = 32 - sum
let total = result * 5
let divide = total / 10
- Click the Run button to run it.
The results...