{"id":2628,"date":"2026-02-05T08:00:16","date_gmt":"2026-02-05T08:00:16","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2628"},"modified":"2026-02-05T08:00:16","modified_gmt":"2026-02-05T08:00:16","slug":"chapter-22-swift-operators","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-22-swift-operators\/","title":{"rendered":"Chapter 22: Swift Operators"},"content":{"rendered":"<h3 dir=\"auto\">1. What are Operators?<\/h3>\n<p dir=\"auto\">Operators are <strong>special symbols<\/strong> (or sometimes words) that perform an operation on one, two, or three values.<\/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 a = 10 + 5          \/\/ + is an operator\r\nlet isAdult = age &gt;= 18 \/\/ &gt;= is an operator\r\nlet name = first + \" \" + last   \/\/ + for strings\r\nlet isEven = number % 2 == 0    \/\/ %, == are operators<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Swift has several categories of operators. We\u2019ll look at the most important ones in detail.<\/p>\n<h3 dir=\"auto\">2. Arithmetic Operators (+ \u2013 * \/ %)<\/h3>\n<p dir=\"auto\">These work with numbers (Int, Double, Float, Decimal).<\/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 a = 10\r\nlet b = 3\r\n\r\nprint(a + b)    \/\/ 13\r\nprint(a - b)    \/\/ 7\r\nprint(a * b)    \/\/ 30\r\nprint(a \/ b)    \/\/ 3   (integer division \u2192 drops decimal part)\r\nprint(a % b)    \/\/ 1   (remainder)\r\n\r\n\/\/ With floating point\r\nlet x = 10.0\r\nlet y = 3.0\r\n\r\nprint(x \/ y)    \/\/ 3.3333333333333335\r\nprint(x.truncatingRemainder(dividingBy: y))   \/\/ 1.0<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Compound assignment operators<\/strong> (very common)<\/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 = 100\r\nscore += 50     \/\/ same as score = score + 50\r\nscore -= 20\r\nscore *= 2\r\nscore \/= 4\r\nscore %= 7\r\n\r\nprint(score)    \/\/ 60<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Real-life 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 itemPrice: Decimal = 1499.99\r\nlet quantity = 3\r\nlet taxRate: Decimal = 0.18\r\n\r\nvar subtotal = itemPrice * Decimal(quantity)\r\nsubtotal += subtotal * taxRate   \/\/ add tax\r\n\r\nprint(\"Total: \u20b9\\(subtotal)\")     \/\/ Total: \u20b95311.9702<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Comparison Operators (== != &gt; &lt; &gt;= &lt;=)<\/h3>\n<p dir=\"auto\">Return Bool (true or false)<\/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 age = 19\r\n\r\nprint(age == 18)     \/\/ false\r\nprint(age != 18)     \/\/ true\r\nprint(age &gt; 18)      \/\/ true\r\nprint(age &gt;= 18)     \/\/ true\r\nprint(age &lt; 16)      \/\/ false\r\nprint(age &lt;= 21)     \/\/ true<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Very common mistake beginners make:<\/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 name = \"Rahul\"\r\n\r\n\/\/ Wrong:\r\nif name == \"rahul\" { \u2026 }          \/\/ false \u2013 case sensitive!\r\n\r\n\/\/ Better:\r\nif name.lowercased() == \"rahul\" { \u2026 }<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Logical Operators (&amp;&amp; || !)<\/h3>\n<p dir=\"auto\">Combine or invert Bool values<\/p>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"sm\">Operator<\/th>\n<th data-col-size=\"md\">Name<\/th>\n<th data-col-size=\"lg\">Meaning<\/th>\n<th data-col-size=\"lg\">Example<\/th>\n<th data-col-size=\"xl\">Result<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"sm\">&amp;&amp;<\/td>\n<td data-col-size=\"md\">AND<\/td>\n<td data-col-size=\"lg\">Both must be true<\/td>\n<td data-col-size=\"lg\">age &gt;= 18 &amp;&amp; hasLicense<\/td>\n<td data-col-size=\"xl\">true only if both true<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">`<\/td>\n<td data-col-size=\"md\"><\/td>\n<td data-col-size=\"lg\">`<\/td>\n<td data-col-size=\"lg\">OR<\/td>\n<td data-col-size=\"xl\">At least one must be true<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">!<\/td>\n<td data-col-size=\"md\">NOT<\/td>\n<td data-col-size=\"lg\">Reverses the value<\/td>\n<td data-col-size=\"lg\">!isLoggedIn<\/td>\n<td data-col-size=\"xl\">true \u2192 false, false \u2192 true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Real example \u2013 form validation<\/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 email = \"aarav@example.com\"\r\nlet password = \"Secure123!\"\r\n\r\nlet emailValid = email.contains(\"@\") &amp;&amp; email.contains(\".\")\r\nlet passwordLong = password.count &gt;= 8\r\nlet passwordsMatch = true \/\/ assume confirmed\r\n\r\nlet canSubmit = emailValid &amp;&amp; passwordLong &amp;&amp; passwordsMatch\r\n\r\nprint(canSubmit)   \/\/ true<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Short-circuit evaluation<\/strong> (very important to understand)<\/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 user: User? = nil\r\n\r\n\/\/ This is safe \u2013 right side not evaluated if left is false\r\nif user != nil &amp;&amp; user!.isPremium {\r\n    print(\"Show premium badge\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. 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 score = 0\r\nscore = 100\r\nscore = score + 50<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Compound versions<\/strong> (already seen):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>score += 50\r\nscore -= 20\r\nscore *= 2\r\nscore \/= 5\r\nscore %= 7<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Ternary Conditional Operator (?:) \u2013 very popular<\/h3>\n<p dir=\"auto\">Short version of if-else<\/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 age = 19\r\n\r\nlet status = age &gt;= 18 ? \"Adult\" : \"Minor\"\r\nprint(status)   \/\/ Adult\r\n\r\n\/\/ Real example\r\nlet buttonTitle = isLoggedIn ? \"Logout\" : \"Login\"<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>More readable version with multiple conditions<\/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 temperature = 36.8\r\n\r\nlet message = temperature &lt; 35 ? \"Too cold \u2744\ufe0f\"\r\n            : temperature &gt; 38 ? \"Fever \ud83d\ude37\"\r\n            : \"Normal \ud83d\udc4d\"\r\n\r\nprint(message)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">7. Nil-Coalescing Operator (??)<\/h3>\n<p dir=\"auto\">Very common when dealing with optionals<\/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 nickname: String? = nil\r\nlet displayName = nickname ?? \"Guest\"\r\nprint(displayName)   \/\/ Guest\r\n\r\nlet score: Int? = nil\r\nlet points = score ?? 0<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Real usage \u2013 user profile<\/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 userBio: String? = nil\r\nlet bioText = userBio ?? \"No bio yet\"<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">8. Range Operators<\/h3>\n<p dir=\"auto\">Very common in loops and switches<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Closed range: includes both ends\r\nfor i in 1...5 {        \/\/ 1,2,3,4,5\r\n    print(i)\r\n}\r\n\r\n\/\/ Half-open range: up to but not including end\r\nfor i in 0..&lt;10 {       \/\/ 0 to 9\r\n    print(i)\r\n}\r\n\r\n\/\/ One-sided ranges (Swift 4+)\r\nlet numbers = [10, 20, 30, 40, 50]\r\nlet firstThree = numbers[..&lt;3]      \/\/ [10, 20, 30]\r\nlet fromThird = numbers[2...]       \/\/ [30, 40, 50]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">9. Identity Operators (=== !==) \u2013 only for classes<\/h3>\n<p dir=\"auto\">These check whether two references point to the <strong>same instance<\/strong> (not value equality)<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>class Person {\r\n    var name: String\r\n    init(name: String) { self.name = name }\r\n}\r\n\r\nlet p1 = Person(name: \"Rahul\")\r\nlet p2 = Person(name: \"Rahul\")\r\nlet p3 = p1\r\n\r\nprint(p1 === p2)    \/\/ false \u2013 different objects\r\nprint(p1 === p3)    \/\/ true  \u2013 same object<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">10. Quick Reference Table \u2013 Most Used Operators<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">Category<\/th>\n<th data-col-size=\"md\">Operator(s)<\/th>\n<th data-col-size=\"lg\">Example<\/th>\n<th data-col-size=\"lg\">Result \/ Use Case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">Arithmetic<\/td>\n<td data-col-size=\"md\">+ &#8211; * \/ %<\/td>\n<td data-col-size=\"lg\">price * quantity<\/td>\n<td data-col-size=\"lg\">Calculations<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Compound assignment<\/td>\n<td data-col-size=\"md\">+= -= *= \/= %=<\/td>\n<td data-col-size=\"lg\">score += 100<\/td>\n<td data-col-size=\"lg\">Updating variables<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Comparison<\/td>\n<td data-col-size=\"md\">== != &gt; &lt; &gt;= &lt;=<\/td>\n<td data-col-size=\"lg\">age &gt;= 18<\/td>\n<td data-col-size=\"lg\">Conditions<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Logical<\/td>\n<td data-col-size=\"md\">`&amp;&amp;<\/td>\n<td data-col-size=\"lg\"><\/td>\n<td data-col-size=\"lg\">!`<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Ternary<\/td>\n<td data-col-size=\"md\">?:<\/td>\n<td data-col-size=\"lg\">isLoggedIn ? &#8220;Logout&#8221; : &#8220;Login&#8221;<\/td>\n<td data-col-size=\"lg\">Short if-else<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Nil-coalescing<\/td>\n<td data-col-size=\"md\">??<\/td>\n<td data-col-size=\"lg\">name ?? &#8220;Anonymous&#8221;<\/td>\n<td data-col-size=\"lg\">Safe optional unwrapping<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Range<\/td>\n<td data-col-size=\"md\">&#8230; ..&lt;<\/td>\n<td data-col-size=\"lg\">1&#8230;10, 0..&lt;count<\/td>\n<td data-col-size=\"lg\">Loops, switches, slices<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Identity<\/td>\n<td data-col-size=\"md\">=== !==<\/td>\n<td data-col-size=\"lg\">object1 === object2<\/td>\n<td data-col-size=\"lg\">Check same instance (classes only)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">11. Small Practice \u2013 Combine Operators<\/h3>\n<p dir=\"auto\">Try to write these using operators:<\/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. Calculate final price with 18% tax\r\nlet basePrice = 999.99\r\nlet taxRate = 0.18\r\n\/\/ your code here\r\n\r\n\/\/ 2. Check if user can vote (age 18+ and Indian citizen)\r\nlet age = 19\r\nlet isIndian = true\r\n\/\/ your code here\r\n\r\n\/\/ 3. Show \"Welcome back\" if logged in, otherwise \"Sign in\"\r\nlet isLoggedIn = true\r\n\/\/ your code here<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Would you like to go deeper into any specific operator group?<\/p>\n<ul dir=\"auto\">\n<li>Arithmetic &amp; compound assignment in real calculations<\/li>\n<li>Logical operators in complex conditions<\/li>\n<li>Ternary &amp; nil-coalescing patterns<\/li>\n<li>Ranges in SwiftUI \/ loops \/ switch<\/li>\n<li>Or move to another topic (control flow, functions, optionals\u2026)<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me \u2014 we\u2019ll continue in the same detailed, clear, teacher-like style \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What are Operators? Operators are special symbols (or sometimes words) that perform an operation on one, two, or three values. Swift let a = 10 + 5 \/\/ + is an operator let&#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-2628","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2628","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=2628"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2628\/revisions"}],"predecessor-version":[{"id":2629,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2628\/revisions\/2629"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}