{"id":2677,"date":"2026-02-05T10:00:33","date_gmt":"2026-02-05T10:00:33","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2677"},"modified":"2026-02-05T10:00:33","modified_gmt":"2026-02-05T10:00:33","slug":"chapter-46-swift-else","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-46-swift-else\/","title":{"rendered":"Chapter 46: Swift else"},"content":{"rendered":"<h3 dir=\"auto\">1. What does else actually mean?<\/h3>\n<p dir=\"auto\">else means <strong>\u201cotherwise\u201d<\/strong> or <strong>\u201cin all other cases\u201d<\/strong>.<\/p>\n<p dir=\"auto\">It is the <strong>fallback branch<\/strong> that runs when the if condition (and all else if conditions) are <strong>false<\/strong>.<\/p>\n<p dir=\"auto\">So the full mental model is:<\/p>\n<ul dir=\"auto\">\n<li><strong>if<\/strong> this is true \u2192 do this<\/li>\n<li><strong>else if<\/strong> that is true \u2192 do that<\/li>\n<li><strong>else<\/strong> (none of the above are true) \u2192 do this instead<\/li>\n<\/ul>\n<p dir=\"auto\">The else block is <strong>optional<\/strong> \u2014 you can have an if without any else.<\/p>\n<h3 dir=\"auto\">2. Very first examples \u2013 let&#8217;s feel what else does<\/h3>\n<p dir=\"auto\"><strong>Example 1 \u2013 Very basic<\/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 = 26\r\n\r\nif temperature &gt; 30 {\r\n    print(\"It's hot \ud83e\udd75\")\r\n} else {\r\n    print(\"The weather is comfortable \ud83d\ude0a\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul dir=\"auto\">\n<li>temperature = 33 \u2192 prints &#8220;It&#8217;s hot \ud83e\udd75&#8221;<\/li>\n<li>temperature = 26 \u2192 prints &#8220;The weather is comfortable \ud83d\ude0a&#8221;<\/li>\n<\/ul>\n<p dir=\"auto\">The else block is the <strong>safety net<\/strong> \u2014 it catches every case that did <strong>not<\/strong> satisfy the if.<\/p>\n<p dir=\"auto\"><strong>Example 2 \u2013 No else (nothing happens when false)<\/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 age = 16\r\n\r\nif age &gt;= 18 {\r\n    print(\"You can vote \u2713\")\r\n}\r\n\/\/ no else \u2192 if age &lt; 18, nothing is printed<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">This is very common when you only care about <strong>one specific case<\/strong> and want to ignore all others.<\/p>\n<h3 dir=\"auto\">3. else with else if \u2014 the chain pattern<\/h3>\n<p dir=\"auto\">This is the <strong>most classic and most frequently written<\/strong> form in real apps.<\/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 score = 82\r\n\r\nif score &gt;= 90 {\r\n    print(\"Excellent! A+ \ud83c\udf89\")\r\n} else if score &gt;= 80 {\r\n    print(\"Very good \u2013 A \ud83d\udc4d\")\r\n} else if score &gt;= 70 {\r\n    print(\"Good \u2013 B \ud83d\ude0a\")\r\n} else if score &gt;= 60 {\r\n    print(\"Pass \u2013 C \ud83d\ude42\")\r\n} else {\r\n    print(\"You need to study more\u2026 let's work together \ud83d\udcda\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>How Swift reads this chain:<\/strong><\/p>\n<ol dir=\"auto\">\n<li>Is score \u2265 90? \u2192 No<\/li>\n<li>Is score \u2265 80? \u2192 Yes \u2192 print &#8220;Very good \u2013 A \ud83d\udc4d&#8221; \u2192 <strong>stops here<\/strong> \u2014 never looks at the rest<\/li>\n<\/ol>\n<p dir=\"auto\"><strong>Important rule #1 about else if chains:<\/strong><\/p>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">The <strong>first true condition wins<\/strong> \u2014 everything after it is ignored.<\/p>\n<\/blockquote>\n<p dir=\"auto\">That\u2019s why you <strong>must<\/strong> put the <strong>highest \/ most specific<\/strong> conditions first.<\/p>\n<p dir=\"auto\"><strong>Wrong order example<\/strong> (very common mistake):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>if score &gt;= 60 {\r\n    print(\"C\")          \/\/ \u2190 this runs for 82 \u2192 wrong!\r\n} else if score &gt;= 80 {\r\n    print(\"A\")          \/\/ \u2190 never reached!\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Correct order<\/strong> \u2014 always highest first:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>if score &gt;= 90 { print(\"A+\") }\r\nelse if score &gt;= 80 { print(\"A\") }\r\nelse if score &gt;= 70 { print(\"B\") }\r\nelse if score &gt;= 60 { print(\"C\") }\r\nelse { print(\"Fail\") }<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Real-life examples \u2014 code you will actually write<\/h3>\n<h4 dir=\"auto\">Example 1 \u2013 User status \/ subscription check<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let isLoggedIn = true\r\nlet hasPremium = false\r\nlet trialDaysLeft = 0\r\n\r\nif !isLoggedIn {\r\n    print(\"Please sign in to continue\")\r\n} else if hasPremium {\r\n    print(\"Welcome back, Premium member! Enjoy all features \ud83c\udf96\ufe0f\")\r\n} else if trialDaysLeft &gt; 0 {\r\n    print(\"Your trial is active \u2014 \\(trialDaysLeft) days left \u2728\")\r\n} else {\r\n    print(\"Welcome! Upgrade to Premium for more features?\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 2 \u2013 Temperature advice (classic teaching + real pattern)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let temp = 34.8\r\nlet feelsLike = 38.2\r\n\r\nif temp &gt; 38 || feelsLike &gt; 42 {\r\n    print(\"Dangerous heat! Stay indoors and drink water \ud83c\udfe0\ud83d\udca7\")\r\n} else if temp &gt; 32 || feelsLike &gt; 36 {\r\n    print(\"Very hot day \u2013 avoid direct sun and stay hydrated \ud83e\udd75\")\r\n} else if temp &gt; 26 {\r\n    print(\"Warm and sunny \u2013 light clothes are perfect \ud83d\ude0e\")\r\n} else if temp &gt; 18 {\r\n    print(\"Pleasant weather \u2013 enjoy the day \ud83c\udf24\ufe0f\")\r\n} else {\r\n    print(\"It's cold \u2013 wear warm layers \u2744\ufe0f\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 3 \u2013 Simple age-based message<\/h4>\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 = 15\r\n\r\nif age &gt;= 18 {\r\n    print(\"You are an adult \u2013 you can vote and drive \ud83d\ude97\")\r\n} else if age &gt;= 13 {\r\n    print(\"You are a teenager \u2013 enjoy school and friends \ud83d\udc69\u200d\ud83c\udf93\")\r\n} else {\r\n    print(\"You are still a child \u2013 lots of fun ahead! \ud83c\udf88\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 4 \u2013 Mini discount calculator<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let totalAmount: Double = 5499.99\r\n\r\nvar discountPercent = 0.0\r\n\r\nif totalAmount &gt;= 10000 {\r\n    discountPercent = 0.20\r\n} else if totalAmount &gt;= 5000 {\r\n    discountPercent = 0.15\r\n} else if totalAmount &gt;= 2000 {\r\n    discountPercent = 0.10\r\n} else {\r\n    discountPercent = 0.05\r\n}\r\n\r\nlet discountAmount = totalAmount * discountPercent\r\nlet finalPrice = totalAmount - discountAmount\r\n\r\nprint(\"Total: \u20b9\\(totalAmount, format: .number.precision(.fractionLength(2)))\")\r\nprint(\"Discount: \\(Int(discountPercent * 100))% \u2192 \u20b9\\(discountAmount, format: .number.precision(.fractionLength(2)))\")\r\nprint(\"You pay: \u20b9\\(finalPrice, format: .number.precision(.fractionLength(2)))\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. Important Details &amp; Best Practices<\/h3>\n<h4 dir=\"auto\">Detail 1 \u2013 Condition must be Bool<\/h4>\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 = 20\r\n\r\n\/\/ Correct\r\nif age &gt;= 18 { \u2026 }\r\n\r\n\/\/ Wrong\r\nif age { \u2026 }           \/\/ \u274c Compile error \u2013 Int is not Bool<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Detail 2 \u2013 Prefer positive conditions when possible<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Harder to read\r\nif !isLoggedIn { showLogin() }\r\n\r\n\/\/ Easier to read\r\nif isLoggedIn {\r\n    showDashboard()\r\n} else {\r\n    showLogin()\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Detail 3 \u2013 Use guard when you want early exit (very modern &amp; recommended)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>func showProfile(user: User?) {\r\n    guard let user else {\r\n        print(\"No user logged in\")\r\n        return\r\n    }\r\n    \r\n    guard user.age &gt;= 13 else {\r\n        print(\"Content not suitable for your age\")\r\n        return\r\n    }\r\n    \r\n    \/\/ happy path \u2013 user exists and is old enough\r\n    print(\"Welcome, \\(user.name)! Here's your profile.\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">guard makes code <strong>much flatter<\/strong> and easier to follow.<\/p>\n<h3 dir=\"auto\">6. Very Common Beginner Mistakes &amp; Fixes<\/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 code<\/th>\n<th data-col-size=\"md\">Correct \/ Better way<\/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 ==<\/td>\n<td data-col-size=\"md\">if age = 18 { \u2026 }<\/td>\n<td data-col-size=\"md\">if age == 18 { \u2026 }<\/td>\n<td data-col-size=\"lg\">= is assignment \u2013 compile error here<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Writing == true \/ == false everywhere<\/td>\n<td data-col-size=\"md\">if isAdult == true { \u2026 }<\/td>\n<td data-col-size=\"md\">if isAdult { \u2026 }<\/td>\n<td data-col-size=\"lg\">Cleaner, more idiomatic<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Wrong order in else-if chain<\/td>\n<td data-col-size=\"md\">else if score &gt;= 90 { &#8220;A+&#8221; } before &gt;=80<\/td>\n<td data-col-size=\"md\">Put highest condition first<\/td>\n<td data-col-size=\"lg\">First true condition wins<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Deep nesting instead of guard<\/td>\n<td data-col-size=\"md\">many nested ifs<\/td>\n<td data-col-size=\"md\">Use guard + early return<\/td>\n<td data-col-size=\"lg\">Flatter, easier to read code<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Comparing optionals directly<\/td>\n<td data-col-size=\"md\">if optional == 10 { \u2026 }<\/td>\n<td data-col-size=\"md\">if let value = optional, value == 10 { \u2026 }<\/td>\n<td data-col-size=\"lg\">optional == 10 usually means you forgot nil<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">7. Quick Reference \u2013 if\u2026else patterns you will use most<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">Situation<\/th>\n<th data-col-size=\"lg\">Recommended pattern<\/th>\n<th data-col-size=\"lg\">Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">Simple yes\/no<\/td>\n<td data-col-size=\"lg\">if condition { \u2026 } else { \u2026 }<\/td>\n<td data-col-size=\"lg\">if isRaining { takeUmbrella() } else { walk() }<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Multiple ranges \/ categories<\/td>\n<td data-col-size=\"lg\">if \u2026 else if \u2026 else if \u2026 else { \u2026 }<\/td>\n<td data-col-size=\"lg\">temperature, grade, score ranges<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Early exit \/ validation<\/td>\n<td data-col-size=\"lg\">guard condition else { return \/ throw \/ \u2026 }<\/td>\n<td data-col-size=\"lg\">guard let user else { return }<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Optional unwrapping + condition<\/td>\n<td data-col-size=\"lg\">if let value = optional, value &gt; 10 { \u2026 }<\/td>\n<td data-col-size=\"lg\">Safe optional handling<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Short one-liner<\/td>\n<td data-col-size=\"lg\">if condition { doSomething() }<\/td>\n<td data-col-size=\"lg\">if isLoading { showSpinner() }<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">8. Small Practice \u2013 Try these<\/h3>\n<ol dir=\"auto\">\n<li>Write weather advice code:\n<ul dir=\"auto\">\n<li>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">38 \u2192 &#8220;Extreme heat! Stay inside&#8221;<\/p>\n<\/blockquote>\n<\/li>\n<li>32\u201338 \u2192 &#8220;Very hot \u2013 drink water&#8221;<\/li>\n<li>25\u201332 \u2192 &#8220;Warm day&#8221;<\/li>\n<li>15\u201325 \u2192 &#8220;Pleasant&#8221;<\/li>\n<li>else \u2192 &#8220;Cold&#8221;<\/li>\n<\/ul>\n<\/li>\n<li>Simple login check:\n<ul dir=\"auto\">\n<li>not logged in \u2192 &#8220;Please sign in&#8221;<\/li>\n<li>logged in, not premium \u2192 &#8220;Welcome! Get Premium?&#8221;<\/li>\n<li>logged in + premium \u2192 &#8220;Welcome Premium member!&#8221;<\/li>\n<\/ul>\n<\/li>\n<li>Mini grade calculator:\n<ul dir=\"auto\">\n<li>\u2265 90 \u2192 &#8220;A+&#8221;<\/li>\n<li>80\u201389 \u2192 &#8220;A&#8221;<\/li>\n<li>70\u201379 \u2192 &#8220;B&#8221;<\/li>\n<li>60\u201369 \u2192 &#8220;C&#8221;<\/li>\n<li>below 60 \u2192 &#8220;Fail&#8221;<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p dir=\"auto\">Paste your code here if you want feedback or want to see cleaner\/more modern versions!<\/p>\n<p dir=\"auto\">What would you like to explore next?<\/p>\n<ul dir=\"auto\">\n<li>guard statement in depth (very important companion to if)<\/li>\n<li>switch statement (often cleaner than long if\u2026else if chains)<\/li>\n<li>if combined with <strong>optionals<\/strong> (if let, if case let, etc.)<\/li>\n<li>Conditional logic in <strong>SwiftUI<\/strong> (showing\/hiding views)<\/li>\n<li>Or move to another topic (loops, functions, arrays, optionals\u2026)<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me \u2014 we\u2019ll continue in the same clear, patient, detailed style \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What does else actually mean? else means \u201cotherwise\u201d or \u201cin all other cases\u201d. It is the fallback branch that runs when the if condition (and all else if conditions) are false. So the&#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-2677","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2677","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=2677"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2677\/revisions"}],"predecessor-version":[{"id":2678,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2677\/revisions\/2678"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}