{"id":192,"date":"2022-01-04T11:43:02","date_gmt":"2022-01-04T11:43:02","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=192"},"modified":"2022-01-04T11:43:02","modified_gmt":"2022-01-04T11:43:02","slug":"scala-conditional-statements","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-conditional-statements\/","title":{"rendered":"Scala &#8211; Conditional Statements"},"content":{"rendered":"<p>In this chapter, you will learn how to use conditional statements. We would cover the following conditional statements:<\/p>\n<ol>\n<li><a href=\"#t1\">if Statement<\/a><\/li>\n<li><a href=\"#t2\">if-else Statement<\/a><\/li>\n<li><a href=\"#t3\">Multiple if-else Statement<\/a><\/li>\n<li><a href=\"#t4\">Nested if-else Statement<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. if Statement<\/strong><\/h4>\n<p>The if statement is use to evaluate a conditional expression and has the syntax:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>expression<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #888888;\">\/\/ Execute if true<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>If the expression evaluates to true, then the code inside the block is executed. Otherwise, the code is skipped.<\/p>\n<p>An example program is given below. Try to enter and run it using your compiler.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ConditionDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">var<\/span> age <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n      <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span> age <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">15<\/span> <span style=\"color: #333333;\">){<\/span>\r\n         println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"He is young\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n      <span style=\"color: #333333;\">}<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. if-else Statement<\/strong><\/h4>\n<p>For the if-else statement, we have a conditional expression, then we have two blocks of code:<\/p>\n<ul>\n<li><strong>if block<\/strong> &#8211; executes if the expression evaluates to true<\/li>\n<li><strong>else block<\/strong> &#8211; executes if the expression evaluates to false<\/li>\n<\/ul>\n<p>The code below is an example of how the if-else statement works<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ConditionDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">var<\/span> age <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n      <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span> age <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">15<\/span> <span style=\"color: #333333;\">){<\/span>\r\n         println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"He is a child\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n      <span style=\"color: #333333;\">}<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #333333;\">{<\/span>\r\n         println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"He is an adult\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n      <span style=\"color: #333333;\">}<\/span>\r\n\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Multiple if-else Statement<\/strong><\/h4>\n<p>In this case, an if statement is followed by one or more else-if-else. This is used to test a series of possible conditions using one statement. An example would be assigning the grades of students based on their scores e.g 70 &#8211; 100 is A, 60 &#8211; 69 is B etc.<\/p>\n<p>Also note that if one of the if statements evaluates to true, then the remaining statements are skipped.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ConditionDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> score <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">50<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span> score <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">70<\/span> <span style=\"color: #333333;\">){<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Excellent!\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> \r\n<span style=\"color: #008800; font-weight: bold;\">    else<\/span> <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span> score <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">49<\/span> <span style=\"color: #333333;\">){<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Average performance\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> \r\n<span style=\"color: #008800; font-weight: bold;\">    else<\/span> <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span> score <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">30<\/span> <span style=\"color: #333333;\">){<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Not so good\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> \r\n<span style=\"color: #008800; font-weight: bold;\">    else<\/span><span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"He Failed!!\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>In this example, the compiler starts by checking the first conditional expression (score == 70), then subsequently checks other expression if the result of the previous is false. So the output would be &#8220;Average Performance&#8221;. At this point, subsequent expressions are skipped.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Nested if-else Statement<\/strong><\/h4>\n<p>In this case we would have if-else statements nested within another if-else statement. Here, each of the if blocks is treated separately.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">NestedDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">var<\/span> a <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">100<\/span><span style=\"color: #333333;\">;<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">var<\/span> b <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">200<\/span><span style=\"color: #333333;\">;<\/span>\r\n      \r\n      <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span> a <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">100<\/span> <span style=\"color: #333333;\">){<\/span>\r\n         <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span> b <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">200<\/span> <span style=\"color: #333333;\">){<\/span>\r\n            println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"A = 100 and B = 200\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n         <span style=\"color: #333333;\">}<\/span>\r\n      <span style=\"color: #333333;\">}<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this chapter, you will learn how to use conditional statements. We would cover the following conditional statements: if Statement if-else Statement Multiple if-else Statement &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-192","post","type-post","status-publish","format-standard","hentry","category-scala-programming"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=192"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/192\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}