Example # 01
We are starting our article with the most basic example of Scala to discuss the use of break functions in the loops. We have to use a file containing the scala extension at its end to avoid any problems in the near future. We have created the “test.scala” file and successfully used it within the text editor.
Firstly, we are importing the scala.util.control package here to smoothly utilizes the breaks in the code. After that, we initialized an object “test” to be used for execution. We have initialized variable “n” with value 0 and list variable “l” with a total of 8 integer elements. Another variable object, “b” has been calling the “Breaks” class to use the break() function in the loops. The object “b” would be used to call the breakable function before the loops to avoid any error in execution.
Within this breakable function, we have created a “for” loop iterating itself using each value of list “l” in the variable “n”. The println() statement within it would be used to display the iteration number, i.e., the value of a list. Along with that, we have been using the “if” statement to define a condition for breaking the loop. If the loop iteration variable “n” meets the value “9”, it will be calling the break statement with the break class object “b”. The loop will be broken right at the moment, and we will be out of the loop executing the rest of the code. The last println() function statement would display that we are out of the loop, breaking it successfully.
Let’s save this easiest example code of this guide first via the simple Ctrl+S key and open the shell for compilation purposes. After saving the scala file, we have compiled it with Scala’s “scalac” compiler. The object file “test” was created at the same location. We have executed the compiled code using the name of an object “test” used in the code. The output is presented on our console screen. The loop has been iterated, and the first 5 odd values have been printed, while when the loop meets the value 9, it breaks. The last print message has been displayed.
Example # 02
We have discussed the use of breaks on a single loop while programming in the Scala language. The single loops are not as trouble makers as much as the nested loops are. So, within this example now, we will be breaking the nested loops using the same scala.util.control package of Scala. Therefore, starting the new program within the same scala file. We have to start the code with the import keyword used for the import of the “scala.util.control” library. The object “test” has been started with the main() function definition.
We have initialized two variables, “n” and “m” with the value “0”. Then, we initialized two separate lists, l1 and l2, using the List() function. The first list contains 3 integer values while the other contains 5 integer values in it. Two more variables, “out” and “in” are initialized using the Breaks class of scala. These two variables are used as the break objects to be used for breaking the loops. The ‘out’ object would be used for the outer loop, and the “in” object would be used for the inner loop break.
So, we have been calling the breakable function with the “out” variable object to avoid any exception in the outer loop of this scala program. Within this breakable function, we have initialized a nested “for” loop. The outer loop has been taking values from the first list “l1” in a variable “n” for its iteration. The println() statement within the outer loop has been used to display the value of variable “n” i.e., each list element. Now, we have been calling the breakable() function again with the “in” variable object” to avoid the inner loop exceptions and make it clear that we are going to use the inner loop now.
The inner “for” loop has been iterating itself using the values of the second list “l2” in the variable “m”. Its println() statement would be displaying each of its list values or iteration numbers one after another. It also contains an “if” statement to define a condition within the inner loop to break this nested loop (loop within a loop). If the value of variable iterator “m” reaches 8, the inner loop will be broken by the variable object “in” using the break function statement of Scala. The inner loop breaks every time it reaches 8 until the outer loop completes its all iterations. Let’s just save and run this code for now.
On compilation and execution of the scala code, we have got the long output at the console screen. The outer loop has been completed successfully, while the inner loop breaks every time it meets 8.
Within our article’s introduction, we have discussed the need to use break statements of functions in programming languages nowadays. For this, we have discussed the use of scala.util.control package along with its break functions, i.e., breakable and break statement. We have discussed the use of break statements to break the simple and nested loops in Scala.