{"id":2675,"date":"2026-02-05T09:56:46","date_gmt":"2026-02-05T09:56:46","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2675"},"modified":"2026-02-05T09:57:06","modified_gmt":"2026-02-05T09:57:06","slug":"chapter-45-swift-if","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-45-swift-if\/","title":{"rendered":"Chapter 45: Swift if"},"content":{"rendered":"<h3 dir=\"auto\">1. What does if actually do?<\/h3>\n<p dir=\"auto\">if is the <strong>most basic decision-making tool<\/strong> in programming.<\/p>\n<p dir=\"auto\">It lets your code ask a <strong>yes\/no question<\/strong> and then choose what to do based on the answer.<\/p>\n<p dir=\"auto\">The condition inside if must be something that results in <strong>true<\/strong> or <strong>false<\/strong> (a Bool value).<\/p>\n<p dir=\"auto\">Basic form:<\/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    \/\/ This code runs only when condition is true\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Very simple first example:<\/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 = 33\r\n\r\nif temperature &gt; 30 {\r\n    print(\"It's hot today \ud83e\udd75\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">If you run this code when temperature is 33 \u2192 you see the message. If you change it to 25 \u2192 nothing happens (because condition is false).<\/p>\n<h3 dir=\"auto\">2. Adding else \u2014 what to do when condition is false<\/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 = 28\r\n\r\nif temperature &gt; 30 {\r\n    print(\"It's hot \ud83e\udd75 \u2013 drink water!\")\r\n} else {\r\n    print(\"Nice weather \ud83d\ude0a \u2013 good for a walk\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Now the code <strong>always does something<\/strong> \u2014 either the hot message or the nice weather message.<\/p>\n<h3 dir=\"auto\">3. else if \u2014 checking several possibilities<\/h3>\n<p dir=\"auto\">When you have <strong>more than two possible situations<\/strong>, you 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 = 26\r\n\r\nif temperature &gt; 38 {\r\n    print(\"Extreme heat warning! Stay indoors \ud83c\udfe0\ud83d\udca7\")\r\n} else if temperature &gt; 32 {\r\n    print(\"Very hot \u2013 lots of water and shade please \ud83e\udd64\")\r\n} else if temperature &gt; 27 {\r\n    print(\"Warm and pleasant day \ud83d\ude0e\")\r\n} else if temperature &gt; 18 {\r\n    print(\"Cool weather \u2013 light jacket \ud83e\udde5\")\r\n} else {\r\n    print(\"It's cold outside \u2744\ufe0f \u2013 wear something warm\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Important rules about else if chains<\/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 following ones<\/strong><\/li>\n<li>Order is very important \u2014 put the <strong>most specific \/ highest value<\/strong> conditions first<\/li>\n<li>The final else (optional) catches <strong>everything that didn&#8217;t match above<\/strong><\/li>\n<\/ul>\n<p dir=\"auto\"><strong>Classic mistake example<\/strong> (very common for beginners):<\/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 = 92\r\n\r\nif score &gt;= 80 {\r\n    print(\"A\")          \/\/ \u2190 this runs \u2192 prints \"A\"\r\n} else if score &gt;= 90 {\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 version<\/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>if score &gt;= 90 {\r\n    print(\"A+\")\r\n} else if score &gt;= 80 {\r\n    print(\"A\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Real-life examples \u2014 the kind of code you will write every day<\/h3>\n<h4 dir=\"auto\">Example 1 \u2013 User 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 Enjoy all features.\")\r\n    } else {\r\n        print(\"Welcome back! Consider upgrading to Premium \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 Basic age check (very common)<\/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 = 17\r\n\r\nif age &gt;= 18 {\r\n    print(\"You can vote \u2713\")\r\n    print(\"You can buy lottery tickets\")\r\n} else if age &gt;= 16 {\r\n    print(\"You can get a learner's driving license\")\r\n} else {\r\n    print(\"You're still young \u2014 enjoy school! \ud83c\udf92\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 3 \u2013 Weather advice (classic beginner-to-intermediate exercise)<\/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 temperature = 34.5\r\nlet isRaining = false\r\n\r\nvar advice = \"\"\r\n\r\nif temperature &gt; 38 {\r\n    advice = \"Dangerous heat! Stay indoors and drink water \ud83c\udfe0\ud83d\udca7\"\r\n} else if temperature &gt; 32 {\r\n    advice = \"Very hot day \u2013 avoid direct sun \ud83e\udd75\"\r\n} else if temperature &gt; 25 {\r\n    advice = \"Warm and pleasant \u2013 light clothes are fine \ud83d\ude0e\"\r\n} else if temperature &gt; 15 {\r\n    advice = \"Cool weather \u2013 a jacket might be good \ud83e\udde5\"\r\n} else {\r\n    advice = \"It's cold outside \u2013 wear warm layers \u2744\ufe0f\"\r\n}\r\n\r\nif isRaining {\r\n    advice += \"\\nDon't forget an umbrella \u2614\"\r\n}\r\n\r\nprint(\"Weather advice for today:\")\r\nprint(advice)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 4 \u2013 Mini grade 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 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 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 for 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 greetUser(user: User?) {\r\n    guard let user else {\r\n        print(\"No user provided\")\r\n        return\r\n    }\r\n    \r\n    guard user.age &gt;= 18 else {\r\n        print(\"User is underage\")\r\n        return\r\n    }\r\n    \r\n    \/\/ happy path \u2013 user exists and is adult\r\n    print(\"Welcome, \\(user.name)!\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">guard makes code <strong>flatter<\/strong> and <strong>much easier to read<\/strong> than deep nesting.<\/p>\n<h3 dir=\"auto\">6. Very Common Beginner Mistakes &amp; How to Fix Them<\/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 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 if actually do? if is the most basic decision-making tool in programming. It lets your code ask a yes\/no question and then choose what to do based on the answer. 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-2675","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2675","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=2675"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2675\/revisions"}],"predecessor-version":[{"id":2676,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2675\/revisions\/2676"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}