- iOS 12 Programming for Beginners
- Craig Clayton
- 257字
- 2021-07-02 15:04:53
Operations with our integers
In our Playground, we know that age is an int, but with Int, we also can write arithmetic expressions using numbers, variables/constants, operators, and parentheses. Let's start with addition, subtraction, multiplication, and division. Add the following into Xcode:

So, sum added two integers (+ operator) together, totaling 43 in our preceding example. Then, we subtracted (- operator) sum from 32 to create a result (?11, in our example). After that, we took the result and multiplied (* operator) it by 5 (see -55 in the Results Panel). All of this is pretty basic math; however, you may have noticed something different with our division equation (/ operator). When you divide two integers, the result is the third integer. So, instead of -55 divided by 10 equals -5.5, our result was -5. To get the correct floating-point value of -5.5, we need to make our division value a Double. Therefore, let's add the following:
let divide2 = Double(total) / 10
After adding the preceding line of code, your code should look something like this:

All of these operations might look familiar to you, but there is one with which you might not be familiar, and that is the remainder operator. The remainder operator returns the remainder when a number is divided by another.
So, for example, 7 divided by 3 equals 2.33. When we apply the remainder operator, we get back 1. Add the following to Playgrounds:
let mod = 7 % 3
Now, your code should look something like this:

- Aptana Studio Beginner's Guide
- 物聯網網絡安全及應用
- INSTANT PhpStorm Starter
- Hands-On Chatbots and Conversational UI Development
- 網絡故障現場處理實踐(第4版)
- 物聯網關鍵技術及應用
- WordPress 5 Complete
- SD-WAN架構與技術(第2版)
- Building RESTful Web Services with Spring 5(Second Edition)
- Yii Application Development Cookbook(Second Edition)
- Getting Started with Grunt:The JavaScript Task Runner
- 通信原理及MATLAB/Simulink仿真
- Bonita Open Solution 5.x Essentials
- Scala Design Patterns.
- 網絡工程實施技術與方案大全