官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 徐州市| 彩票| 闽清县| 盖州市| 惠东县| 黔东| 西青区| 营山县| 姜堰市| 淳化县| 诸暨市| 囊谦县| 肇东市| 工布江达县| 河池市| 银川市| 阿合奇县| 安福县| 墨玉县| 大名县| 开鲁县| 诸暨市| 根河市| 墨玉县| 天全县| 八宿县| 息烽县| 杨浦区| 安庆市| 穆棱市| 彰武县| 临汾市| 延寿县| 资阳市| 崇左市| 吉安县| 沙坪坝区| 陵水| 开阳县| 志丹县| 天气|