{"id":196,"date":"2022-01-04T18:00:52","date_gmt":"2022-01-04T18:00:52","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=196"},"modified":"2022-01-04T18:00:52","modified_gmt":"2022-01-04T18:00:52","slug":"scala-loops","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-loops\/","title":{"rendered":"Scala &#8211; Loops"},"content":{"rendered":"<p>In this lesson we would learn how to work with loops in Scala. A loop allows you to execute a particular block of code repeatedly depending on some condition.<\/p>\n<p>The three types of loops we would cover are<\/p>\n<ol>\n<li><a href=\"#t1\">for loop<\/a><\/li>\n<li><a href=\"#t2\">while loop<\/a><\/li>\n<li><a href=\"#t3\">do-while loop<\/a><\/li>\n<li><a href=\"#t4\">Break Statement<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. for loop<\/strong><\/h4>\n<p>The for loop executes a block (or blocks) of code a number of times. The number of times the code would executed is determined by the loop variable.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/For loop example<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ForLoopDemo<\/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> count <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>count <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> to <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Current value of x: \"<\/span> <span style=\"color: #333333;\">+<\/span> count<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>If you execute the code above, it would print the text &#8220;Current value of x: 1&#8221; 10 time each for each different value of x.<\/p>\n<p>The statement<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">(<\/span>count <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> to <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>is called a generator statement and it can be modified by replacing the keyword &#8220;to&#8221; with &#8220;untill&#8221; like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">(<\/span>count <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> until <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Multiple Ranges<\/strong><\/p>\n<p>Scala allows you to specify multiple ranges separated by a semicolon(;). In this case, the loop will iterate through all possible combinations of the given ranges. Think of it as nested for loop. Below is an example:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/For loop with multiple ranges<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ForLoopDemo<\/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> x <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> y <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>x <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> to <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">;<\/span> y <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> to <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Current value of x: \"<\/span> <span style=\"color: #333333;\">+<\/span> x<span style=\"color: #333333;\">);<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Current value of y: \"<\/span> <span style=\"color: #333333;\">+<\/span> y<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>The output will be as shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Current value of x: 1\r\nCurrent value of y: 1\r\nCurrent value of x: 1\r\nCurrent value of y: 2\r\nCurrent value of x: 1\r\nCurrent value of y: 3\r\nCurrent value of x: 2\r\nCurrent value of y: 1\r\nCurrent value of x: 2\r\nCurrent value of y: 2\r\nCurrent value of x: 2\r\nCurrent value of y: 3\r\nCurrent value of x: 3\r\nCurrent value of y: 1\r\nCurrent value of x: 3\r\nCurrent value of y: 2\r\nCurrent value of x: 3\r\nCurrent value of y: 3\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. while loop<\/strong><\/h4>\n<p>While loop repeats a group of statements while a specified condition is true. It checks the loop condition before executing the loop the first time and subsequently. So the statements are executed so long as the condition is true. The syntax for the while loop is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span>conditional expression<span style=\"color: #333333;\">){<\/span>\r\n   statement<span style=\"color: #333333;\">(<\/span>s<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Note that the different between the for loop and the<strong> while loop<\/strong> is that the <strong>for loop<\/strong> will execute at least once, but the while loop may not even execute at all. The code below is an example of while loop.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/While loop example<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">WhileLoopDemo<\/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> x <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Counting down at \"<\/span> <span style=\"color: #333333;\">+<\/span> x<span style=\"color: #333333;\">);<\/span>\r\n      x <span style=\"color: #008800; font-weight: bold;\">=<\/span> x <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/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>The output of the code would be a countdown from 5 to 1<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. do-while loop<\/strong><\/h4>\n<p>The different here is that the do-while loop tests the condition at the bottom of the loop. Therefore, the do-while loop would execute at least one time. The syntax for the do-while loop is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">do<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   statement<span style=\"color: #333333;\">(<\/span>s<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span> \r\n<span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span> loop condition <span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The code below is an example of how the do-while loop 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;\">DoWhileDemo<\/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> x <span style=\"color: #008800; font-weight: bold;\">=<\/span> 2<span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n      <span style=\"color: #888888;\">\/\/ do loop execution<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">do<\/span> <span style=\"color: #333333;\">{<\/span>\r\n         println<span style=\"color: #333333;\">(<\/span> <span style=\"background-color: #fff0f0;\">\"Value of x: \"<\/span> <span style=\"color: #333333;\">+<\/span> x <span style=\"color: #333333;\">);<\/span>\r\n         x <span style=\"color: #008800; font-weight: bold;\">=<\/span> x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span>\r\n      <span style=\"color: #333333;\">}<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span> a <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">50<\/span> <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=\"t4\">4. Break Statement<\/strong><\/h4>\n<p>Break statement is one of the loop control statements. The break statement terminates the current loop and transfers execution to the statement immediately following the loop. However, Scala does not originally support break statement. To achieve break\u00a0 statement in Scala, you need to do two things:<\/p>\n<ul>\n<li>Place the loop inside a <strong><em>loop.breakable<\/em><\/strong> block<\/li>\n<li>Then you need to use the statement<em><strong> loop.break<\/strong><\/em>.<\/li>\n<\/ul>\n<p>The program below shows how the break statement works in Scala.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">scala.util.control._<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">BreakDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  \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>score <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">val<\/span> scores <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">List<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">60<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">65<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">70<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">75<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">80<\/span><span style=\"color: #333333;\">);<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">val<\/span> loop <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Breaks<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n    loop<span style=\"color: #333333;\">.<\/span>breakable <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span> score <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> scores<span style=\"color: #333333;\">){<\/span>\r\n        println<span style=\"color: #333333;\">(<\/span> <span style=\"background-color: #fff0f0;\">\"Value of score: \"<\/span> <span style=\"color: #333333;\">+<\/span> score <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;\">70<\/span> <span style=\"color: #333333;\">){<\/span>\r\n          loop<span style=\"color: #333333;\">.<\/span>break<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    println<span style=\"color: #333333;\">(<\/span> <span style=\"background-color: #fff0f0;\">\"Executes after the loop\"<\/span> <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 lesson we would learn how to work with loops in Scala. A loop allows you to execute a particular block of code repeatedly &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":[32],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-loops"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/196","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=196"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":198,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/196\/revisions\/198"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}