{"id":2671,"date":"2026-02-05T09:50:18","date_gmt":"2026-02-05T09:50:18","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2671"},"modified":"2026-02-05T09:50:18","modified_gmt":"2026-02-05T09:50:18","slug":"chapter-43-swift-if-else","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-43-swift-if-else\/","title":{"rendered":"Chapter 43: Swift If&#8230;Else"},"content":{"rendered":"<h3 dir=\"auto\">1. What is if\u2026else?<\/h3>\n<p dir=\"auto\">if\u2026else is the <strong>most fundamental way<\/strong> to make decisions in code.<\/p>\n<p dir=\"auto\">It answers the question:<\/p>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">\u201cIf this condition is true, do this block of code. Otherwise (else), do something different (or do nothing).\u201d<\/p>\n<\/blockquote>\n<p dir=\"auto\">Basic structure:<\/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 condition {\r\n    \/\/ code to run if condition is true\r\n} else {\r\n    \/\/ code to run if condition is false\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">The else part is <strong>optional<\/strong>.<\/p>\n<h3 dir=\"auto\">2. Very first examples \u2013 try these right now<\/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 temperature = 32\r\n\r\nif temperature &gt; 30 {\r\n    print(\"It's really hot \ud83e\udd75\")\r\n} else {\r\n    print(\"The weather is nice \ud83c\udf24\ufe0f\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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 = 17\r\n\r\nif age &gt;= 18 {\r\n    print(\"You can vote \u2713\")\r\n}\r\n\/\/ no else \u2192 do nothing if false<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let isRaining = true\r\n\r\nif isRaining {\r\n    print(\"Take umbrella \u2614\")\r\n} else {\r\n    print(\"Nice day for a walk \ud83c\udf1e\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. else if \u2014 checking multiple conditions<\/h3>\n<p dir=\"auto\">When you have <strong>more than two possibilities<\/strong>, use else if.<\/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 = 28\r\n\r\nif temperature &gt; 35 {\r\n    print(\"Extreme heat warning! \ud83d\udd25\")\r\n} else if temperature &gt; 30 {\r\n    print(\"Very hot day \u2600\ufe0f\")\r\n} else if temperature &gt; 25 {\r\n    print(\"Warm and pleasant \ud83d\ude0a\")\r\n} else if temperature &gt; 15 {\r\n    print(\"Cool weather \ud83e\udde5\")\r\n} else {\r\n    print(\"It's cold! \u2744\ufe0f\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Important rules about else if:<\/strong><\/p>\n<ul dir=\"auto\">\n<li>Swift checks conditions <strong>from top to bottom<\/strong><\/li>\n<li>As soon as <strong>one condition is true<\/strong>, it runs that block and <strong>skips all the rest<\/strong><\/li>\n<li>You can have as many else if as you want<\/li>\n<li>The final else (optional) catches <strong>everything else<\/strong><\/li>\n<\/ul>\n<h3 dir=\"auto\">4. Real-life examples \u2013 the kind of code you will actually write<\/h3>\n<h4 dir=\"auto\">Example 1 \u2013 Simple login status<\/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\n\r\nif isLoggedIn {\r\n    if hasPremium {\r\n        print(\"Welcome back, Premium member! \ud83c\udf96\ufe0f\")\r\n    } else {\r\n        print(\"Welcome back! Consider Premium for more features \u2728\")\r\n    }\r\n} else {\r\n    print(\"Please sign in to continue\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 2 \u2013 Form validation (very common in apps)<\/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 email = \"aarav@example.com\"\r\nlet password = \"Secure123!\"\r\nlet termsAccepted = true\r\n\r\nvar errors: [String] = []\r\n\r\nif email.isEmpty {\r\n    errors.append(\"Email is required\")\r\n} else if !email.contains(\"@\") || !email.contains(\".\") {\r\n    errors.append(\"Please enter a valid email\")\r\n}\r\n\r\nif password.isEmpty {\r\n    errors.append(\"Password is required\")\r\n} else if password.count &lt; 8 {\r\n    errors.append(\"Password must be at least 8 characters\")\r\n}\r\n\r\nif !termsAccepted {\r\n    errors.append(\"You must accept the terms\")\r\n}\r\n\r\nif errors.isEmpty {\r\n    print(\"Form is valid \u2013 creating account\u2026\")\r\n} else {\r\n    print(\"Please fix the following errors:\")\r\n    for error in errors {\r\n        print(\"\u2022 \\(error)\")\r\n    }\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 3 \u2013 Temperature \/ weather advice<\/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 temperatureCelsius = 33.2\r\nlet isRaining = false\r\n\r\nif temperatureCelsius &gt;= 38 {\r\n    print(\"Dangerous heat! Stay indoors \ud83c\udfe0\ud83d\udca7\")\r\n} else if temperatureCelsius &gt;= 32 {\r\n    print(\"Very hot \u2014 drink lots of water \ud83e\udd64\")\r\n} else if temperatureCelsius &gt;= 25 {\r\n    print(\"Warm day \u2014 light clothes \ud83d\ude0e\")\r\n} else if temperatureCelsius &gt;= 15 {\r\n    print(\"Pleasant weather \u2014 perfect for a walk \ud83c\udf33\")\r\n} else {\r\n    print(\"It's cold \u2014 wear a jacket \ud83e\udde5\")\r\n}\r\n\r\nif isRaining {\r\n    print(\"Don't forget umbrella \u2614\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 4 \u2013 Score \/ grade system (very typical)<\/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 percentage = 82.5\r\n\r\nlet grade: String\r\n\r\nif percentage &gt;= 90 {\r\n    grade = \"A+\"\r\n} else if percentage &gt;= 80 {\r\n    grade = \"A\"\r\n} else if percentage &gt;= 70 {\r\n    grade = \"B\"\r\n} else if percentage &gt;= 60 {\r\n    grade = \"C\"\r\n} else if percentage &gt;= 50 {\r\n    grade = \"D\"\r\n} else {\r\n    grade = \"F\"\r\n}\r\n\r\nprint(\"You scored \\(percentage)% \u2192 Grade: \\(grade)\")<\/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 Conditions 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 Avoid very deep nesting<\/h4>\n<p dir=\"auto\">Bad (hard to read):<\/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 user != nil {\r\n    if user!.isPremium {\r\n        if user!.hasSubscription {\r\n            \/\/ \u2026\r\n        }\r\n    }\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Better:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>guard let user else { return }\r\nguard user.isPremium else { return }\r\nguard user.hasSubscription else { return }\r\n\/\/ happy path<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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=\"lg\">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=\"lg\">if age == 18 { \u2026 }<\/td>\n<td data-col-size=\"lg\">= is assignment \u2014 compile error here<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Writing == true everywhere<\/td>\n<td data-col-size=\"md\">if isAdult == true { \u2026 }<\/td>\n<td data-col-size=\"lg\">if isAdult { \u2026 }<\/td>\n<td data-col-size=\"lg\">Cleaner &amp; idiomatic<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Forgetting else if chain order<\/td>\n<td data-col-size=\"md\">if score &gt;= 80 { &#8220;A&#8221; } else if score &gt;= 90 { &#8220;A+&#8221; }<\/td>\n<td data-col-size=\"lg\">Put higher conditions 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\/early return<\/td>\n<td data-col-size=\"md\">many nested ifs<\/td>\n<td data-col-size=\"lg\">Use guard or 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 incorrectly<\/td>\n<td data-col-size=\"md\">if optional == 10 { \u2026 }<\/td>\n<td data-col-size=\"lg\">if let value = optional, value == 10 { \u2026 }<\/td>\n<td data-col-size=\"lg\">Direct comparison with nil is usually wrong<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">7. Quick Reference \u2013 if 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=\"sm\">Situation<\/th>\n<th data-col-size=\"xl\">Recommended pattern<\/th>\n<th data-col-size=\"lg\">Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"sm\">Simple yes\/no<\/td>\n<td data-col-size=\"xl\">if condition { \u2026 } else { \u2026 }<\/td>\n<td data-col-size=\"lg\">if isRaining { \u2026 } else { \u2026 }<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Multiple ranges<\/td>\n<td data-col-size=\"xl\">if \u2026 else if \u2026 else if \u2026 else { \u2026 }<\/td>\n<td data-col-size=\"lg\">grade \/ temperature checks<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Early exit \/ validation<\/td>\n<td data-col-size=\"xl\">guard condition else { return }<\/td>\n<td data-col-size=\"lg\">guard let user else { return }<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Optional unwrapping + check<\/td>\n<td data-col-size=\"xl\">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=\"sm\">Short one-liner<\/td>\n<td data-col-size=\"xl\">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 code that prints different messages based on temperature:\n<ul dir=\"auto\">\n<li>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">35 \u2192 &#8220;Extreme heat&#8221;<\/p>\n<\/blockquote>\n<\/li>\n<li>30\u202635 \u2192 &#8220;Very hot&#8221;<\/li>\n<li>25\u202630 \u2192 &#8220;Warm&#8221;<\/li>\n<li>else \u2192 &#8220;Cool&#8221;<\/li>\n<\/ul>\n<\/li>\n<li>Create a simple login check:\n<ul dir=\"auto\">\n<li>if not logged in \u2192 &#8220;Please sign in&#8221;<\/li>\n<li>if logged in but not premium \u2192 &#8220;Welcome! Get Premium?&#8221;<\/li>\n<li>if premium \u2192 &#8220;Welcome Premium member!&#8221;<\/li>\n<\/ul>\n<\/li>\n<li>Write a mini grade calculator: marks \u2265 90 \u2192 &#8220;A+&#8221; 80\u201389 \u2192 &#8220;A&#8221; 70\u201379 \u2192 &#8220;B&#8221; 60\u201369 \u2192 &#8220;C&#8221; below 60 \u2192 &#8220;Fail&#8221;<\/li>\n<\/ol>\n<p dir=\"auto\">Paste your code here if you want feedback or want to see cleaner 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>Combining if with <strong>optionals<\/strong> (if let, if case let, etc.)<\/li>\n<li>if in <strong>SwiftUI<\/strong> (conditional 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 is if\u2026else? if\u2026else is the most fundamental way to make decisions in code. It answers the question: \u201cIf this condition is true, do this block of code. Otherwise (else), do something different&#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-2671","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2671","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=2671"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2671\/revisions"}],"predecessor-version":[{"id":2672,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2671\/revisions\/2672"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}