{"id":2634,"date":"2026-02-05T08:08:25","date_gmt":"2026-02-05T08:08:25","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2634"},"modified":"2026-02-05T08:08:25","modified_gmt":"2026-02-05T08:08:25","slug":"chapter-25-swift-assignment-operators","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-25-swift-assignment-operators\/","title":{"rendered":"Chapter 25: Swift Assignment Operators"},"content":{"rendered":"<h3 dir=\"auto\">1. What are Assignment Operators?<\/h3>\n<p dir=\"auto\">An <strong>assignment operator<\/strong> is used to <strong>assign a value<\/strong> to a variable or constant (or more generally: to a place in memory).<\/p>\n<p dir=\"auto\">The most basic one is the single = sign:<\/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 name = \"Priya\"          \/\/ assign string \"Priya\" to constant name\r\nvar score = 0               \/\/ assign 0 to variable score<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">But Swift has a whole family of <strong>assignment operators<\/strong> \u2014 the most important ones are the <strong>compound assignment operators<\/strong>.<\/p>\n<h3 dir=\"auto\">2. The Basic Assignment Operator: =<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var temperature = 28.5          \/\/ initial value\r\ntemperature = 29.2              \/\/ change value later\r\n\r\nlet maxUsers = 1000             \/\/ can only be assigned once\r\n\/\/ maxUsers = 1500             \/\/ \u274c compile error \u2013 cannot re-assign let<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Important rule (very important mindset):<\/strong><\/p>\n<ul dir=\"auto\">\n<li>Use let when the value <strong>will never change<\/strong> after the first assignment<\/li>\n<li>Use var when the value <strong>will be replaced<\/strong> with a new value later<\/li>\n<\/ul>\n<p dir=\"auto\">This is <strong>not<\/strong> just style \u2014 it\u2019s one of the strongest safety features of Swift.<\/p>\n<h3 dir=\"auto\">3. Compound Assignment Operators \u2014 the ones you use every day<\/h3>\n<p dir=\"auto\">These combine an arithmetic operation with assignment in one step.<\/p>\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=\"md\">Equivalent to<\/th>\n<th data-col-size=\"xl\">Most common real use case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"xs\">+=<\/td>\n<td data-col-size=\"lg\">Add and assign<\/td>\n<td data-col-size=\"md\">x = x + y<\/td>\n<td data-col-size=\"xl\">Accumulating scores, totals, counters<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">-=<\/td>\n<td data-col-size=\"lg\">Subtract and assign<\/td>\n<td data-col-size=\"md\">x = x &#8211; y<\/td>\n<td data-col-size=\"xl\">Reducing lives, applying discounts<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">*=<\/td>\n<td data-col-size=\"lg\">Multiply and assign<\/td>\n<td data-col-size=\"md\">x = x * y<\/td>\n<td data-col-size=\"xl\">Scaling values, doubling, taxes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">\/=<\/td>\n<td data-col-size=\"lg\">Divide and assign<\/td>\n<td data-col-size=\"md\">x = x \/ y<\/td>\n<td data-col-size=\"xl\">Averaging, shrinking values<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">%=<\/td>\n<td data-col-size=\"lg\">Remainder and assign<\/td>\n<td data-col-size=\"md\">x = x % y<\/td>\n<td data-col-size=\"xl\">Cycling values, checking patterns<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Live examples \u2013 try these right now<\/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 points = 150\r\n\r\npoints += 100           \/\/ 250\r\npoints -= 30            \/\/ 220\r\npoints *= 2             \/\/ 440\r\npoints \/= 4             \/\/ 110\r\npoints %= 7             \/\/ 5   (110 % 7 = 5)\r\n\r\nprint(points)           \/\/ 5<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Real-life examples \u2013 how compound assignment is actually used<\/h3>\n<h4 dir=\"auto\">Example 1 \u2013 Shopping cart total<\/h4>\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         \/\/ charger\r\n\r\ncartTotal += cartTotal * 0.18   \/\/ add 18% GST\r\n\r\nprint(\"Final cart: \u20b9\\(cartTotal)\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 2 \u2013 Game score &amp; lives<\/h4>\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 = 0\r\nvar lives = 3\r\n\r\n\/\/ player collects coin\r\nscore += 100\r\nlives -= 1      \/\/ hit obstacle\r\n\r\n\/\/ bonus round\r\nscore *= 2\r\n\r\nprint(\"Score: \\(score), Lives: \\(lives)\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 3 \u2013 Progress bar \/ percentage<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var completedTasks = 0\r\nlet totalTasks = 12\r\n\r\ncompletedTasks += 1     \/\/ finish one task\r\ncompletedTasks += 3     \/\/ finish three more\r\n\r\nlet progress = Double(completedTasks) \/ Double(totalTasks) * 100\r\n\r\nprint(\"Progress: \\(progress)%\")     \/\/ e.g. 33.333...<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 4 \u2013 Cycling \/ wrapping values (using %=)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var currentPage = 0\r\nlet totalPages = 5\r\n\r\n\/\/ user clicks \"next\"\r\ncurrentPage += 1\r\ncurrentPage %= totalPages   \/\/ wraps around: 0,1,2,3,4,0,1,...\r\n\r\nprint(\"Showing page \\(currentPage + 1)\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. 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=\"lg\">Wrong \/ Risky code<\/th>\n<th data-col-size=\"md\">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\">Using = instead of += in loops<\/td>\n<td data-col-size=\"lg\">total = total + price every time<\/td>\n<td data-col-size=\"md\">total += price<\/td>\n<td data-col-size=\"lg\">Shorter, clearer, less typing mistakes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Trying to use += on let<\/td>\n<td data-col-size=\"lg\">let count = 0; count += 1<\/td>\n<td data-col-size=\"md\">Use var if value must change<\/td>\n<td data-col-size=\"lg\">let cannot be reassigned<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Doing money math with += on Double<\/td>\n<td data-col-size=\"lg\">total += 0.1 many times<\/td>\n<td data-col-size=\"md\">Use Decimal for money<\/td>\n<td data-col-size=\"lg\">Avoids floating-point rounding errors<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Writing x =+ 5 (wrong order)<\/td>\n<td data-col-size=\"lg\">score =+ 100<\/td>\n<td data-col-size=\"md\">score += 100<\/td>\n<td data-col-size=\"lg\">=+ is not valid syntax<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Forgetting to declare variable first<\/td>\n<td data-col-size=\"lg\">count += 1 without previous declaration<\/td>\n<td data-col-size=\"md\">var count = 0; count += 1<\/td>\n<td data-col-size=\"lg\">Must exist before modifying<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Quick Reference \u2013 Assignment 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=\"md\">Equivalent longer form<\/th>\n<th data-col-size=\"xl\">Most common real-world use case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"xs\">=<\/td>\n<td data-col-size=\"lg\">Simple assignment<\/td>\n<td data-col-size=\"md\">\u2014<\/td>\n<td data-col-size=\"xl\">Initial value, one-time setting<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">+=<\/td>\n<td data-col-size=\"lg\">Add and assign<\/td>\n<td data-col-size=\"md\">x = x + y<\/td>\n<td data-col-size=\"xl\">Accumulating totals, scores, counters<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">-=<\/td>\n<td data-col-size=\"lg\">Subtract and assign<\/td>\n<td data-col-size=\"md\">x = x &#8211; y<\/td>\n<td data-col-size=\"xl\">Decreasing lives, applying discounts<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">*=<\/td>\n<td data-col-size=\"lg\">Multiply and assign<\/td>\n<td data-col-size=\"md\">x = x * y<\/td>\n<td data-col-size=\"xl\">Doubling values, calculating tax\/growth<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">\/=<\/td>\n<td data-col-size=\"lg\">Divide and assign<\/td>\n<td data-col-size=\"md\">x = x \/ y<\/td>\n<td data-col-size=\"xl\">Averaging, shrinking progress bars<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">%=<\/td>\n<td data-col-size=\"lg\">Remainder and assign<\/td>\n<td data-col-size=\"md\">x = x % y<\/td>\n<td data-col-size=\"xl\">Cycling pages, checking even\/odd, wrapping<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">7. Small Practice \u2013 Try these right now<\/h3>\n<p dir=\"auto\">Copy and complete these:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ 1. Shopping cart with discount &amp; tax\r\nvar total: Decimal = 0\r\n\r\ntotal += 2499.99        \/\/ laptop\r\ntotal += 799.00         \/\/ mouse\r\n\r\n\/\/ apply 15% discount\r\n\/\/ add 18% GST\r\n\/\/ print final amount\r\n\r\n\/\/ 2. Progress counter\r\nvar completed = 0\r\nlet totalSteps = 10\r\n\r\ncompleted += 4\r\ncompleted += 3\r\n\/\/ use \/= to calculate fraction completed\r\n\/\/ print percentage (multiply by 100)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Would you like to go deeper into any part?<\/p>\n<ul dir=\"auto\">\n<li>More real examples with <strong>Decimal<\/strong> (money, invoices, discounts)<\/li>\n<li>How to <strong>combine<\/strong> arithmetic + comparison + assignment<\/li>\n<li>Using arithmetic operators in <strong>loops<\/strong> and <strong>animations<\/strong><\/li>\n<li>Common patterns in <strong>games<\/strong>, <strong>forms<\/strong>, <strong>shopping apps<\/strong><\/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 keep going in the same clear, patient, teacher-like style \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What are Assignment Operators? An assignment operator is used to assign a value to a variable or constant (or more generally: to a place in memory). The most basic one is the single&#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-2634","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2634","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=2634"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2634\/revisions"}],"predecessor-version":[{"id":2635,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2634\/revisions\/2635"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}