Scala Break (Scala Loop Control Statement) – Breaking Nested Loop
Scala course with real-time projects Start Now!!
So, we discussed three kinds of loops in Scala. In this Scala Break tutorial, we will discuss this syntax & example of Scala Break statement. Moreover, we will learn how to break nested loop in Scala. Along with this, we will solve these queries: what about controlling these loops? What if we wanted to break out of one midway?
So, let’s start the Scala Break Statements Tutorial.
What is Scala Loop Control Statement?
Scala loop control statement lets us exercise a little more control over a loop. It prevents normal execution.
When we leave a scope, Scala destroys all automatic objects created in that scope. Actually, Scala did not support this functionality until version 2.8. Technically, there are no ‘break’ or ‘continue’ statements in Scala, unlike Java. But we must have something, right? Let’s first look at what this means.
Scala Break
When we’re inside a loop, and we encounter a break, the loop exhausts(terminates), and we skip to the first statement outside the loop.
The Syntax of Scala Break
This is the syntax for Scala break statement:
import scala.util.control._ val loop=new Breaks; loop.breakable { for(...) { //Code loop.break; } }
Scala Break Example
So, what is all this about? let’s take an example to clear the air.
import scala.util.control._ object Main extends App { val loop=new Breaks     val nums=List(1,2,3,4,5) var a=0 loop.breakable { for(a<-nums) { println(a) if(a==3) { loop.break } } } }
The output is:
1
2
3
Any doubt yet in break statement in Scala? Please Comment
Breaking out of a Nested Loop in Scala
For this to work on nested loops, we do this:
import scala.util.control._ object Main extends App { val inner=new Breaks val outer=new Breaks val nums1=List(1,2,3,4) val nums2=List(5,6,7,8) var a=0 var b=0 outer.breakable { for(a<-nums1) { println(a) inner.breakable { for(b<-nums2) { println(b) if(b==7) { inner.break } } } if(a==3) { outer.break } } } }
And the output is:
1
5
6
7
2
5
6
7
3
5
6
7
Whoa, that was confusing, what happened? Let’s see this step by step.
Initially, a and b are 0. The outer for-loop prints 1 from nums1. Then, the inner for-loop prints 5,6, and 7. It stops at 7 because of the break statement. Then, it checks if a==3. It isn’t. So, it gets back to the outer for-loop.
Now, a takes the value 2 from nums1. This goes on until a takes on the value 3. It prints 3, and then prints 5,6, and 7 from the inner for-loop. Now, it checks if a==3. This is true this time. So, it gets out of the outer loop. Hence, the output.
Conclusion
Not as convenient as Java, of course, but the ‘break’ in Scala still works. What’s your excuse? Please share your reviews in the comment box.
Did you like this article? If Yes, please give DataFlair 5 Stars on Google
This page was confusing. There was a neat example(s) provided but no description about breakable and break. Also a short commentary on what was to be achieved with the code used for nested for loop with break/breakable would have helped
Hello,
for the example in the “5. Scala Break Example” section, firstable there is an issue in the first line you wrote coMtrol instead of coNtrol. And I got another output.
1
2
3
1
1
2
3
2
1
2
3
3
Thanks to you !
Hey, Ororo
Thanks for the observation on Scala Break. It seems we cluttered the code when editing this lesson while also misspelling control. It is because of people like you that we get an honest chance to improve. We have made the necessary changes.
Regards,
DataFlair