{"id":32075,"date":"2024-11-01T09:05:28","date_gmt":"2024-11-01T09:05:28","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32075"},"modified":"2024-11-01T11:33:25","modified_gmt":"2024-11-01T11:33:25","slug":"unity-basics-course-conditional-statements-switch","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32075\/","title":{"rendered":"Unity Basics Course: Conditional Statements &#8211; Switch"},"content":{"rendered":"<p>Hello! In this course, we will take a closer look at the <strong>switch<\/strong> statement, one of the <strong>conditional statements<\/strong> in Unity. Conditional statements play a very important role in programming languages and can be applied in various situations in Unity. Through conditional statements, we can control the flow of the program and make it behave differently based on specific conditions.<\/p>\n<h2>1. What is a Conditional Statement?<\/h2>\n<p>A conditional statement is a statement that changes the flow of program execution based on whether a certain condition is true or false. By using conditional statements, a program can react differently depending on user input or the state of variables. Common conditional statements include <strong>if<\/strong>, <strong>else<\/strong>, and <strong>switch<\/strong>.<\/p>\n<h2>2. Introduction to the Switch Statement<\/h2>\n<p>The <strong>switch<\/strong> statement is a conditional statement that helps you write multiple conditions easily. It is particularly useful for branching based on the values that a specific variable can take. It often has better readability and is easier to manage than <strong>if-else<\/strong> statements.<\/p>\n<h3>2.1 Basic Structure of the Switch Statement<\/h3>\n<pre><code>switch (variable) {\n    case value1:\n        \/\/ Code corresponding to value1\n        break;\n    case value2:\n        \/\/ Code corresponding to value2\n        break;\n    default:\n        \/\/ Code to be executed if no cases match\n}<\/code><\/pre>\n<p>Looking at the above basic structure, the <strong>case<\/strong> statement is executed based on the value of the <strong>variable<\/strong>. The <strong>break<\/strong> statement serves to terminate the switch statement after executing the current case. If there is no <strong>break<\/strong> statement, the execution will continue to the next case.<\/p>\n<h2>3. Example of Using the Switch Statement<\/h2>\n<p>Now, let&#8217;s see how the switch statement is actually used through a simple example. In this example, we will write a game that outputs different messages based on the player&#8217;s score.<\/p>\n<pre><code>using UnityEngine;\n\npublic class ScoreManager : MonoBehaviour {\n    void Start() {\n        int playerScore = 70; \/\/ Player score\n        \n        switch (playerScore) {\n            case 0:\n                Debug.Log(\"No score.\");\n                break;\n            case 1:\n            case 2:\n            case 3:\n                Debug.Log(\"Score is very low.\");\n                break;\n            case 4:\n            case 5:\n            case 6:\n                Debug.Log(\"Score is average.\");\n                break;\n            case 7:\n            case 8:\n            case 9:\n                Debug.Log(\"Score is high.\");\n                break;\n            default:\n                Debug.Log(\"Excellent score!\");\n                break;\n        }\n    }\n}<\/code><\/pre>\n<p>The above code outputs various messages based on the player&#8217;s score. If the score is 0, the message &#8220;No score.&#8221; is displayed, and if the score is between 1 and 3, the message &#8220;Score is very low.&#8221; is displayed. If the score is 10 or more, the message &#8220;Excellent score!&#8221; is shown.<\/p>\n<h2>4. Points to Note When Using the Switch Statement<\/h2>\n<ul>\n<li><strong>Data Types<\/strong>: The variable used in the switch statement can be of <strong>integer<\/strong>, <strong>character<\/strong>, or <strong>string<\/strong> types and can also be used with enumerated types (enum).<\/li>\n<li><strong>Break Statement<\/strong>: A <strong>break<\/strong> statement must be added at the end of each case. Forgetting to do this can lead to unintended outcomes, as execution will fall through to the next case.<\/li>\n<li><strong>Duplicate Case Values<\/strong>: Each case value must be different from all others; duplicates will cause errors.<\/li>\n<\/ul>\n<h2>5. Switch Statement vs. If-Else Statement<\/h2>\n<p>Let&#8217;s compare the switch statement and the if-else statement. Both statements serve to branch code to be executed based on conditions, but they have their own advantages and disadvantages depending on the situation in which they are used.<\/p>\n<h3>5.1 Advantages of If-Else Statements<\/h3>\n<p>If-else statements can be combined with more complex conditions (e.g., using comparison operators) to handle specific conditions more precisely.<\/p>\n<h3>5.2 Advantages of Switch Statements<\/h3>\n<p>Switch statements improve code readability and can be easily managed with multiple cases, making them convenient in many scenarios.<\/p>\n<h3>5.3 Performance<\/h3>\n<p>In terms of performance, there is generally not a significant difference, but switch statements can be optimized internally when there are many conditions, potentially leading to better performance.<\/p>\n<h2>6. Conclusion<\/h2>\n<p>In this lesson, we explored the switch statement, which is used in Unity&#8217;s conditional statements. The switch statement is very useful for handling multiple conditions simply. Proper use of switch statements can enhance code readability and make maintenance easier. Try to actively use switch statements as you engage in Unity programming!<\/p>\n<h2>7. Additional Resources<\/h2>\n<p>Below are additional resources related to the switch statement.<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/MonoBehaviour.html\">Unity Scripting API &#8211; MonoBehaviour<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/keywords\/switch\">C# Switch Statement<\/a><\/li>\n<li><a href=\"https:\/\/www.learncsharp.com\/Conditions\">Learn C# &#8211; Conditions<\/a><\/li>\n<\/ul>\n<p>In the next lesson, we will cover more diverse conditional statements and their applications in Unity programming. Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this course, we will take a closer look at the switch statement, one of the conditional statements in Unity. Conditional statements play a very important role in programming languages and can be applied in various situations in Unity. Through conditional statements, we can control the flow of the program and make it behave &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32075\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Unity Basics Course: Conditional Statements &#8211; Switch&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[135],"tags":[],"class_list":["post-32075","post","type-post","status-publish","format-standard","hentry","category-unity-basic"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unity Basics Course: Conditional Statements - Switch - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/32075\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unity Basics Course: Conditional Statements - Switch - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this course, we will take a closer look at the switch statement, one of the conditional statements in Unity. Conditional statements play a very important role in programming languages and can be applied in various situations in Unity. Through conditional statements, we can control the flow of the program and make it behave &hellip; \ub354 \ubcf4\uae30 &quot;Unity Basics Course: Conditional Statements &#8211; Switch&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32075\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:05:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:33:25+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32075\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32075\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Unity Basics Course: Conditional Statements &#8211; Switch\",\"datePublished\":\"2024-11-01T09:05:28+00:00\",\"dateModified\":\"2024-11-01T11:33:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32075\/\"},\"wordCount\":606,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Unity Basic\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32075\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32075\/\",\"name\":\"Unity Basics Course: Conditional Statements - Switch - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:05:28+00:00\",\"dateModified\":\"2024-11-01T11:33:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32075\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32075\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32075\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unity Basics Course: Conditional Statements &#8211; Switch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unity Basics Course: Conditional Statements - Switch - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/32075\/","og_locale":"ko_KR","og_type":"article","og_title":"Unity Basics Course: Conditional Statements - Switch - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this course, we will take a closer look at the switch statement, one of the conditional statements in Unity. Conditional statements play a very important role in programming languages and can be applied in various situations in Unity. Through conditional statements, we can control the flow of the program and make it behave &hellip; \ub354 \ubcf4\uae30 \"Unity Basics Course: Conditional Statements &#8211; Switch\"","og_url":"https:\/\/atmokpo.com\/w\/32075\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:05:28+00:00","article_modified_time":"2024-11-01T11:33:25+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32075\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32075\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Unity Basics Course: Conditional Statements &#8211; Switch","datePublished":"2024-11-01T09:05:28+00:00","dateModified":"2024-11-01T11:33:25+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32075\/"},"wordCount":606,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Unity Basic"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32075\/","url":"https:\/\/atmokpo.com\/w\/32075\/","name":"Unity Basics Course: Conditional Statements - Switch - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:05:28+00:00","dateModified":"2024-11-01T11:33:25+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32075\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32075\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32075\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Unity Basics Course: Conditional Statements &#8211; Switch"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=32075"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32075\/revisions"}],"predecessor-version":[{"id":32076,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32075\/revisions\/32076"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}