{"id":2632,"date":"2026-02-05T08:05:38","date_gmt":"2026-02-05T08:05:38","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2632"},"modified":"2026-02-05T08:05:38","modified_gmt":"2026-02-05T08:05:38","slug":"chapter-24-swift-arithmetic-operators","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-24-swift-arithmetic-operators\/","title":{"rendered":"Chapter 24: Swift Arithmetic Operators"},"content":{"rendered":"<h3 dir=\"auto\">1. What are Arithmetic Operators?<\/h3>\n<p dir=\"auto\">Arithmetic operators are the <strong>mathematical symbols<\/strong> you use to perform calculations:<\/p>\n<ul dir=\"auto\">\n<li>+ addition<\/li>\n<li>&#8211; subtraction<\/li>\n<li>* multiplication<\/li>\n<li>\/ division<\/li>\n<li>% remainder (modulo)<\/li>\n<\/ul>\n<p dir=\"auto\">These operators work with <strong>numeric types<\/strong>:<\/p>\n<ul dir=\"auto\">\n<li>Int (whole numbers)<\/li>\n<li>Double (decimals \u2013 most common for real-world math)<\/li>\n<li>Float (less common)<\/li>\n<li>Decimal (exact decimals \u2013 used for money)<\/li>\n<\/ul>\n<h3 dir=\"auto\">2. Basic Arithmetic Operators \u2013 with examples<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let a = 20\r\nlet b = 7\r\n\r\nprint(a + b)    \/\/ 27\r\nprint(a - b)    \/\/ 13\r\nprint(a * b)    \/\/ 140\r\nprint(a \/ b)    \/\/ 2     \u2190 integer division \u2192 drops the decimal part\r\nprint(a % b)    \/\/ 6     \u2190 remainder after division<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Important difference when using floating-point numbers:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let x = 20.0\r\nlet y = 7.0\r\n\r\nprint(x + y)    \/\/ 27.0\r\nprint(x - y)    \/\/ 13.0\r\nprint(x * y)    \/\/ 140.0\r\nprint(x \/ y)    \/\/ 2.857142857142857   \u2190 keeps decimals\r\nprint(x.truncatingRemainder(dividingBy: y))   \/\/ 6.0<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Realistic example \u2013 price calculation<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let basePrice: Double = 1499.99\r\nlet quantity = 3\r\nlet taxRate: Double = 0.18\r\n\r\nlet subtotal = basePrice * Double(quantity)      \/\/ 4499.97\r\nlet taxAmount = subtotal * taxRate               \/\/ 809.9946\r\nlet finalAmount = subtotal + taxAmount           \/\/ 5309.9646\r\n\r\nprint(\"Final price: \u20b9\\(finalAmount)\")            \/\/ Final price: \u20b95309.9646<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Compound Assignment Operators \u2013 very common shorthand<\/h3>\n<p dir=\"auto\">These combine an operation with assignment.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var score = 150\r\n\r\nscore += 100     \/\/ score = score + 100     \u2192 250\r\nscore -= 50      \/\/ \u2192 200\r\nscore *= 2       \/\/ \u2192 400\r\nscore \/= 4       \/\/ \u2192 100\r\nscore %= 7       \/\/ \u2192 2   (remainder after 100 \u00f7 7)\r\n\r\nprint(score)     \/\/ 2<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Real-life pattern \u2013 accumulating totals<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var cartTotal: Decimal = 0\r\n\r\ncartTotal += 1499.99    \/\/ phone\r\ncartTotal += 899.00     \/\/ case\r\ncartTotal += 299.50     \/\/ screen protector\r\n\r\ncartTotal += cartTotal * 0.18   \/\/ add 18% GST\r\n\r\nprint(\"Cart total with tax: \u20b9\\(cartTotal)\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Unary Minus Operator \u2013 changing sign<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let positive = 42\r\nlet negative = -positive        \/\/ -42\r\n\r\nvar temperature = 36.6\r\ntemperature = -temperature      \/\/ -36.6\r\n\r\nlet debt = -5000.75\r\nprint(debt)                     \/\/ -5000.75<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Very common use case \u2013 reversing direction<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var velocity = 50.0     \/\/ moving right\r\nvelocity = -velocity    \/\/ now moving left<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. Increment \/ Decrement Operators \u2013 ++ and &#8212;<\/h3>\n<p dir=\"auto\">These exist in Swift, but they are <strong>very rarely used<\/strong> in modern code.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var count = 0\r\n\r\ncount += 1      \/\/ preferred style\r\ncount += 1\r\n\r\n\/\/ Old style (still works, but most developers avoid it now)\r\ncount++         \/\/ postfix \u2013 returns old value, then increments\r\n++count         \/\/ prefix \u2013 increments first, then returns new value<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Modern &amp; clearer way (what you\u2019ll see in real code 2024\u20132026):<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var index = 0\r\n\r\nfor _ in 1...5 {\r\n    index += 1\r\n    print(\"Item \\(index)\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Why ++ and &#8212; are avoided in modern Swift:<\/strong><\/p>\n<ul dir=\"auto\">\n<li>They can make code harder to read<\/li>\n<li>They behave differently depending on prefix\/postfix<\/li>\n<li>+= 1 is clearer and safer<\/li>\n<\/ul>\n<h3 dir=\"auto\">6. Arithmetic with Decimal \u2013 very important for money<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>import Foundation\r\n\r\nlet coffeePrice: Decimal = 149.50\r\nlet quantity = 4\r\nlet discountPercent: Decimal = 10\r\n\r\nvar total = coffeePrice * Decimal(quantity)                \/\/ 598.00\r\nlet discountAmount = total * (discountPercent \/ 100)        \/\/ 59.80\r\ntotal -= discountAmount                                     \/\/ 538.20\r\n\r\nlet gst = total * 0.18                                      \/\/ 96.876\r\ntotal += gst                                                \/\/ 635.076\r\n\r\nprint(\"Final bill: \u20b9\\(total)\")                              \/\/ Final bill: \u20b9635.076<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Important note:<\/strong><\/p>\n<ul dir=\"auto\">\n<li>Never do money math with Double \u2192 floating-point errors<\/li>\n<li>Always use Decimal for currency<\/li>\n<\/ul>\n<h3 dir=\"auto\">7. Very Common Beginner Mistakes &amp; Correct Habits<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">Mistake<\/th>\n<th data-col-size=\"md\">Wrong \/ Dangerous code<\/th>\n<th data-col-size=\"lg\">Correct \/ Better habit<\/th>\n<th data-col-size=\"lg\">Why?<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">Doing money math with Double<\/td>\n<td data-col-size=\"md\">total += 0.1 many times<\/td>\n<td data-col-size=\"lg\">Use Decimal for prices<\/td>\n<td data-col-size=\"lg\">Avoids 0.30000000000000004 bugs<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Using \/ with Int and expecting decimals<\/td>\n<td data-col-size=\"md\">10 \/ 3 \u2192 3<\/td>\n<td data-col-size=\"lg\">10.0 \/ 3.0 or Double(10) \/ 3<\/td>\n<td data-col-size=\"lg\">Integer division truncates<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Writing x++ in complex expressions<\/td>\n<td data-col-size=\"md\">array[x++]<\/td>\n<td data-col-size=\"lg\">array[x]; x += 1<\/td>\n<td data-col-size=\"lg\">Much clearer, avoids bugs<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Forgetting to convert types<\/td>\n<td data-col-size=\"md\">price * quantity where quantity is Int<\/td>\n<td data-col-size=\"lg\">price * Decimal(quantity)<\/td>\n<td data-col-size=\"lg\">Prevents type mismatch<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Using % with negative numbers carelessly<\/td>\n<td data-col-size=\"md\">-10 % 3<\/td>\n<td data-col-size=\"lg\">Be aware: Swift gives positive remainder<\/td>\n<td data-col-size=\"lg\">-10 % 3 == 2 in Swift<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">8. Quick Reference \u2013 Arithmetic Operators Cheat Sheet<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"xs\">Operator<\/th>\n<th data-col-size=\"lg\">Meaning<\/th>\n<th data-col-size=\"lg\">Example<\/th>\n<th data-col-size=\"md\">Result<\/th>\n<th data-col-size=\"xl\">Most common real use<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"xs\">+<\/td>\n<td data-col-size=\"lg\">Addition<\/td>\n<td data-col-size=\"lg\">a + b<\/td>\n<td data-col-size=\"md\">sum<\/td>\n<td data-col-size=\"xl\">Totals, concatenating strings<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">&#8211;<\/td>\n<td data-col-size=\"lg\">Subtraction<\/td>\n<td data-col-size=\"lg\">a &#8211; b<\/td>\n<td data-col-size=\"md\">difference<\/td>\n<td data-col-size=\"xl\">Discounts, differences<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">*<\/td>\n<td data-col-size=\"lg\">Multiplication<\/td>\n<td data-col-size=\"lg\">price * quantity<\/td>\n<td data-col-size=\"md\">product<\/td>\n<td data-col-size=\"xl\">Subtotal, scaling<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">\/<\/td>\n<td data-col-size=\"lg\">Division<\/td>\n<td data-col-size=\"lg\">total \/ count<\/td>\n<td data-col-size=\"md\">quotient<\/td>\n<td data-col-size=\"xl\">Average, splitting amounts<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">%<\/td>\n<td data-col-size=\"lg\">Remainder (modulo)<\/td>\n<td data-col-size=\"lg\">value % 2<\/td>\n<td data-col-size=\"md\">remainder<\/td>\n<td data-col-size=\"xl\">Checking even\/odd, cycling values<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">+= -= *= \/= %=<\/td>\n<td data-col-size=\"lg\">Compound assignment<\/td>\n<td data-col-size=\"lg\">score += 100<\/td>\n<td data-col-size=\"md\">update<\/td>\n<td data-col-size=\"xl\">Accumulating scores, totals, counters<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">&#8211; (unary)<\/td>\n<td data-col-size=\"lg\">Negation<\/td>\n<td data-col-size=\"lg\">-value<\/td>\n<td data-col-size=\"md\">negative<\/td>\n<td data-col-size=\"xl\">Reverse direction, debt<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">9. Small Practice \u2013 Try these now<\/h3>\n<ol dir=\"auto\">\n<li>Calculate final price: base price = 2499.99 quantity = 2 discount = 20% GST = 18% \u2192 what is the final amount?<\/li>\n<li>Check if a number is divisible by 5 let number = 125 \u2192 use % and print &#8220;Divisible&#8221; or &#8220;Not divisible&#8221;<\/li>\n<li>Update a counter every time an item is added to cart var itemCount = 0 \u2192 add 3 items, then add 2 more<\/li>\n<\/ol>\n<p dir=\"auto\">Paste your attempts if you want feedback!<\/p>\n<p dir=\"auto\">What would you like to explore next?<\/p>\n<ul dir=\"auto\">\n<li>Arithmetic with <strong>Decimal<\/strong> in more detail (money examples)<\/li>\n<li>How to <strong>avoid floating-point surprises<\/strong><\/li>\n<li><strong>Rounding<\/strong> numbers (round, ceil, floor)<\/li>\n<li><strong>Random numbers<\/strong> (Int.random(in:), Double.random(in:))<\/li>\n<li>Or move to another group of operators (comparison, logical, range\u2026)<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me \u2014 we\u2019ll continue in the same detailed, patient, teacher-like style \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What are Arithmetic Operators? Arithmetic operators are the mathematical symbols you use to perform calculations: + addition &#8211; subtraction * multiplication \/ division % remainder (modulo) These operators work with numeric types: Int&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2632","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/comments?post=2632"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2632\/revisions"}],"predecessor-version":[{"id":2633,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2632\/revisions\/2633"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}