{"id":73751,"date":"2023-12-10T14:56:29","date_gmt":"2023-12-10T12:56:29","guid":{"rendered":"https:\/\/codeacademy.bg\/uncategorized-bg\/uslovni-izrazi\/"},"modified":"2024-04-08T20:01:41","modified_gmt":"2024-04-08T17:01:41","slug":"uslovni-izrazi","status":"publish","type":"post","link":"https:\/\/codeacademy.bg\/en\/programirane\/uslovni-izrazi\/","title":{"rendered":"Programming Basics: Conditional Statements if-else and switch"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Conditional expressions in programming are constructs that allow control over the execution of code. Having control and ensuring your program operates as it should is crucial. Two of the most commonly used constructs for controlling execution are the conditional expressions if-else and switch. These tools allow programmers to make decisions based on certain conditions and choose which code to execute. In this article, we will explore these constructs and learn how to use them in various situations.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The if-else Construct<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The if-else construct in programming allows the program to check conditions and execute certain code if those conditions are met. The syntax for if-else is as follows:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`python<\/span><\/p>\n<p><span style=\"font-weight: 400;\">if condition:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0# Code that executes if the condition is true<\/span><\/p>\n<p><span style=\"font-weight: 400;\">else:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0# Code that executes if the condition is false<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`python<\/span><\/p>\n<p><span style=\"font-weight: 400;\">age = 18<\/span><\/p>\n<p><span style=\"font-weight: 400;\">if age &gt;= 18:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(&#8220;You can purchase alcohol.&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">else:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(&#8220;You cannot purchase alcohol.&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In the above example, the program checks if the age is greater than or equal to 18 years and outputs a message accordingly.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Nesting if-else Constructs<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can also nest if-else constructs, which means you can have other if-else constructs inside an if or else block. This is called &#8220;nesting&#8221; and allows you to perform more complex checks.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`python<\/span><\/p>\n<p><span style=\"font-weight: 400;\">age = 18<\/span><\/p>\n<p><span style=\"font-weight: 400;\">driver_license = True<\/span><\/p>\n<p><span style=\"font-weight: 400;\">if age &gt;= 18:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if driver_license == True:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print(&#8220;You can drive a car.&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0else:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print(&#8220;You do not have a driver&#8217;s license.&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">else:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(&#8220;You cannot drive a car.&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This example shows the nesting of if-else constructs when you need to check more than one condition.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The switch (or case) Construct<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The switch construct is specific to some programming languages like C, C++, Java, and others. It allows the program to choose which code to execute based on the value of an expression. An example of a switch construct:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`c<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int day_of_the_week = 3;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">switch (day_of_the_week) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0case 1:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0printf(&#8220;Monday&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0case 2:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0printf(&#8220;Tuesday&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0case 3:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0printf(&#8220;Wednesday&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0default:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0printf(&#8220;Invalid day&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In the above example, the program chooses which code to execute based on the value of the variable day_of_the_week. The break is used to prevent the execution of subsequent cases once a match is found.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In some programming languages, like Python, there isn&#8217;t a standard switch construct. Instead, if-elif-else constructs are used to achieve a similar effect.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Python example with if-elif-else:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`python<\/span><\/p>\n<p><span style=\"font-weight: 400;\">day = 3<\/span><\/p>\n<p><span style=\"font-weight: 400;\">if day == 1:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(&#8220;Monday&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">elif day == 2:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(&#8220;Tuesday&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">elif day == 3:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(&#8220;Wednesday&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">else:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0print(&#8220;The day is not valid&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;`<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When to Use if-else and switch?<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; The if-else construct is suitable when you need to check different conditions and perform different actions based on those conditions. This makes if-else very flexible and suitable for a variety of scenarios.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; The switch construct is useful when you need to check one value against multiple possible values and execute corresponding code for each value. This makes the code more readable and easier to maintain when there are many options.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Conditional expressions if-else and switch are convenient tools for controlling execution in programming. Understanding their essence and how to use them is key to creating programs that make decisions based on various conditions. These frequently used constructs allow the program to execute different code depending on whether a given condition is met or not. These expressions are essential for controlling the program flow and ensuring the correct logic during execution. With practice and experience, you&#8217;ll become very proficient in using these constructs and will be able to create more complex and functional programs.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Conditional expressions in programming are constructs that allow control over the execution of code. Having control and ensuring your program operates as it should is crucial. Two of the most commonly used constructs for controlling execution are the conditional expressions if-else and switch. These tools allow programmers to make decisions based on certain conditions and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":73140,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[97,98],"tags":[],"class_list":["post-73751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programirane","category-programirane-za-nachinaesthi","grve-entry-item","grve-blog-item"],"_links":{"self":[{"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/posts\/73751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/comments?post=73751"}],"version-history":[{"count":1,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/posts\/73751\/revisions"}],"predecessor-version":[{"id":73752,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/posts\/73751\/revisions\/73752"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/media\/73140"}],"wp:attachment":[{"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/media?parent=73751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/categories?post=73751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeacademy.bg\/en\/wp-json\/wp\/v2\/tags?post=73751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}