

{"id":120028,"date":"2023-11-02T18:00:35","date_gmt":"2023-11-02T12:30:35","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=120028"},"modified":"2023-11-02T18:05:13","modified_gmt":"2023-11-02T12:35:13","slug":"swift-decision-making","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/","title":{"rendered":"Swift Decision Making"},"content":{"rendered":"<p>Decision making helps us control the flow of our code based on given conditions. Swift supports various control structures for decision making. In this article, we\u2019ll learn about control statements in Swift, their usage and their implementation with the help of relevant examples.<\/p>\n<h2>Conditional Statements in Swift<\/h2>\n<p>Conditional statements control the code flow based on certain conditions. The following are the control statements we use in Swift Programming.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Statements<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">if statement<\/span><\/td>\n<td><span style=\"font-weight: 400\">It consists of a condition followed by code expressions<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">if-else statement<\/span><\/td>\n<td><span style=\"font-weight: 400\">It consists of a condition along with an optional else statement, which is executed when the condition is false.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">if\u2026else-if\u2026else statement<\/span><\/td>\n<td><span style=\"font-weight: 400\">It consists of a condition along with an optional else if\u2026else statement. It tests several conditions using single if\u2026else statements.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Nested if statement<\/span><\/td>\n<td><span style=\"font-weight: 400\">It consists of if or else if statements enclosed within another if or else if statements.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>if Statements in Swift<\/h3>\n<p>if statements are the condition statements that check if the condition is true. If it is true, then we execute the expression enclosed within it.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/if-statement.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-120850 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/if-statement.webp\" alt=\"if statement\" width=\"600\" height=\"400\" \/><\/a><\/p>\n<p><strong>Syntax of if statements.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">if(condition){\r\n    Expression\r\n}<\/pre>\n<p><strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let year = 2023\r\nif(year != 2020){\r\n    print(\"DataFlair\")\r\n}<\/pre>\n<p><b>Output:<\/b><\/p>\n<p>DataFlair<\/p>\n<h3>if-else Statements in Swift<\/h3>\n<p>if-else statements are the condition statements that check the condition. It executes the expressions accordingly. If it is true, then the expression enclosed within it is executed. If it is false, then the expression within the else scope is executed.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/if-else-condition.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-120849 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/if-else-condition.webp\" alt=\"if else condition\" width=\"600\" height=\"400\" \/><\/a><\/p>\n<p><strong>Syntax of if-else statements.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">if(condition){\r\n    Expression1\r\n}else{\r\n    Expression2\r\n}<\/pre>\n<p><strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let year = 2023\r\nif(year == 2020){\r\n    print(\"Swift\")\r\n}else{\r\n    print(\"DataFlair\")\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>DataFlair<\/p>\n<h3>if\u2026else-if\u2026else Statements in Swift<\/h3>\n<p>if\u2026else-if\u2026else statements are the condition statements which check the condition. It executes the expressions accordingly. If it is true, then the expression enclosed within it is executed if it is false, then the expression within the next else-if or else scope is executed.<\/p>\n<p><strong>Syntax of if\u2026else-if\u2026else statements.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">if(condition){\r\n    Expression1\r\n}else if(condition){\r\n    Expression2\r\n}else{\r\n    Expression3\r\n}<\/pre>\n<p><strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let year = 2023\r\nif(year == 2020){\r\n    print(\"Swift\")\r\n}else if(year == 2023){\r\n    print(\"DataFlair\")\r\n}else{\r\n    print(\"Decision Making\")\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>DataFlair<\/p>\n<h3>Nested if Statements in Swift<\/h3>\n<p>In nested if statements, two or more if statements are enclosed inside each other. Each if statement has its condition. Based on whether the condition is true or not, further expressions are executed.<\/p>\n<p><strong>Syntax of nested if statements.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">if(condition1){\r\n    if(condition2){\r\n        Expression\r\n    }\r\n}<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let year = 2023\r\nif(year != 2020){\r\n    if(year == 2023){\r\n        print(\"DataFlair\")\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>DataFlair<\/p>\n<h3>Switch Statements in Swift<\/h3>\n<p>Switch statements help us manage multiple values for a single variable.<\/p>\n<p><strong>Syntax of switch statements.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">switch expression{\r\ncase expression 1:\r\n    Statement 1\r\n    fallthrough \t\t\/\/optional\r\ncase expression 2:\r\n    Statement 2\r\n    fallthrough \t\t\/\/optional\r\ncase expression 3:\r\n    Statement 3\r\n    fallthrough \t\t\/\/optional\r\ndefault:\r\n    Statement 4\r\n}<\/pre>\n<p>The keyword fallthrough in a case means the expressions\/statements in the next cases should be executed irrespective of whether the condition is met. If this is not used, then the flow comes out of the switch scope when the execution of expression where conditions are met is done.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let year = 2023\r\n\r\nswitch (year){\r\n    case 2020, 2021, 2022:\r\n        print(\"Swift\")\r\n    case 2023:\r\n        print(\"DataFlair\")\r\n    default:\r\n        print(\"Invalid\")\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>DataFlair<\/p>\n<h3>Ternary Operator in Swift<\/h3>\n<p>The ternary operator is a special conditional operator. It operates on three operands: a condition, a true result and a false result. If the condition is true, then it returns the true result. If the condition is false, it returns a false result.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let year = 2023\r\nlet trueOutput = (year == 2023) ? \"DataFlair\" : \"Not DataFlair\"\r\nprint(trueOutput)\r\n\r\nlet falseOutput = (year != 2023) ? \"DataFlair\" : \"Not DataFlair\"\r\nprint(falseOutput)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>DataFlair<br \/>\nNot DataFlair<\/p>\n<h3>Conclusion<\/h3>\n<p>To conclude, decision making is an important part of programming. In Swift, it can be achieved using various ways mentioned above. We can use if, if-else, if\u2026else-if\u2026else or nested if to deal with conditional statements. We can use switch statements to manage multiple variables and conditional statements. Ternary Operator in Swift is a concise and compact way to implement decision making.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Decision making helps us control the flow of our code based on given conditions. Swift supports various control structures for decision making. In this article, we\u2019ll learn about control statements in Swift, their usage&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":120030,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27789],"tags":[28414,28413,28412,20583,14629],"class_list":["post-120028","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift-tutorials","tag-conditional-operators","tag-decision-making-in-swift","tag-swift-decision-making","tag-switch-statements","tag-ternary-operator"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift Decision Making - DataFlair<\/title>\n<meta name=\"description\" content=\"Swift Decision making is an important part of programming. We can use if, if-else, if\u2026else-if\u2026else or nested if to deal with conditional statements.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/swift-decision-making\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift Decision Making - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Swift Decision making is an important part of programming. We can use if, if-else, if\u2026else-if\u2026else or nested if to deal with conditional statements.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/swift-decision-making\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-02T12:30:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-02T12:35:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/swift-decision-making.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift Decision Making - DataFlair","description":"Swift Decision making is an important part of programming. We can use if, if-else, if\u2026else-if\u2026else or nested if to deal with conditional statements.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/","og_locale":"en_US","og_type":"article","og_title":"Swift Decision Making - DataFlair","og_description":"Swift Decision making is an important part of programming. We can use if, if-else, if\u2026else-if\u2026else or nested if to deal with conditional statements.","og_url":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-02T12:30:35+00:00","article_modified_time":"2023-11-02T12:35:13+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/swift-decision-making.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Swift Decision Making","datePublished":"2023-11-02T12:30:35+00:00","dateModified":"2023-11-02T12:35:13+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/"},"wordCount":531,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/swift-decision-making.webp","keywords":["conditional operators","decision making in swift","swift decision making","Switch Statements","Ternary Operator"],"articleSection":["Swift Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/swift-decision-making\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/","url":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/","name":"Swift Decision Making - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/swift-decision-making.webp","datePublished":"2023-11-02T12:30:35+00:00","dateModified":"2023-11-02T12:35:13+00:00","description":"Swift Decision making is an important part of programming. We can use if, if-else, if\u2026else-if\u2026else or nested if to deal with conditional statements.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/swift-decision-making\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/swift-decision-making.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/swift-decision-making.webp","width":1200,"height":628,"caption":"swift decision making"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/swift-decision-making\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Swift Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/swift-tutorials\/"},{"@type":"ListItem","position":3,"name":"Swift Decision Making"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=120028"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120028\/revisions"}],"predecessor-version":[{"id":123208,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120028\/revisions\/123208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/120030"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=120028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=120028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=120028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}