Site icon DataFlair

Scala Break (Scala Loop Control Statement) – Breaking Nested Loop

Loop Control Statement - Scala Break

Loop Control Statement - Scala Break

Free 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.

Break Statement in Scala

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.

Reference

Exit mobile version