Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / Scala
Technical Blog

SCALA Loops

Rate me:
Please Sign up or sign in to vote.
4.87/5 (4 votes)
15 Oct 2015CPOL2 min read 9.3K   2  
How to use the various loops within Scala

This time, we will look at how you can use the various loops within Scala.

For Loops

The for loop in Scala is quite handy, and does some things that other languages do not provide within their for loop symantics.

Range

The simplest for of a for loop is as shown here:

C#
//simple range for loop
var a = 0
for(a <- 1 to 10) {
    println(s"Value of a = $a")
}

Which would give the following results:

image

You can also use until instead of to, which would give you the same results.

Collections

You can also use a for loop to loop through a collection:

C#
//simple for loop through a collection
var list = List(1,2,3,4)
var a = 0
for(a <- list) {
  println(s"Value of a = $a")
}

Which would give the following results:

image

Using Filters

Filters may also be applied to for loops, here is an example:

C#
//simple for loop through a collection
//with a filter for only even numbers
var list = List(1,2,3,4)
var a = 0
for(a <- list if a % 2 ==0) {
  println(s"Value of a = $a")
}

Which when run gives the following results:

image

Using Yield

You can also create two new sequences (if you are a .NET programmer which is really who these posts are aimed at, this would be the same as using a LINQ Select to create a new IEnumerable for some source IEnumerable).

C#
//simple for loop through a collection
//with a filter for only even numbers
//which use yield to create filtered sequence
var list = List(1,2,3,4)
var a = 0
var evens = for {a <- list if a % 2 ==0
            } yield a
 
for(a <- evens) {
  println(s"Value of a = $a")
}

Which when run produces the following results:

image

While Loops

Much the same as other languages, Scala has a while loop that allows us to continue looping while waiting for a certain condition to be met. It is important to note that the while loop checks the condition at the top of the loop, so if the condition is already met the while loop may never run.

C#
//simple while loop
var a = 0
while(a < 10) {
  println(s"Value of a = $a")
  a = a + 1
}

Which when run will give the following result:

image

Do While Loops

Scala also has the popular do-while loop, which unlike the while loop will check the condition at the end of the do-while, so is guaranteed to run at least once. Here is a simple example:

C#
//simple do-while loop
var a = 0
do {
  println(s"Value of a = $a")
  a = a + 1
}while(a < 10) 
C#
//import control packages to give us Breaks
import scala.util.control._
 
//simple breakable for loop
val loop = new Breaks()
var a = 0
loop.breakable {
  for(a <- 0 to 20) {
    println(s"Value of a = $a")
    if(a > 5)
      loop.break()
  }
}
println("Outisde the loop")

Which when run will give you the following results:

image

Breaking Out Of Loops

This is one key area where Scala is very different from .NET. There is no built in break statement available.

However, if you are using a version of Scala after 2.8, then there is a way to use a break statement inside your loops.

You need to import a package to allow this to happen.

The idea is that you wrap your loop in a breakable which is available by using the Breaks class. And then inside your loop, when some condition occurs, you may use Breaks classes break() method.

Let's see an example:

C#
//import control packages to give us Breaks
import scala.util.control._
 
//simple breakable for loop
val loop = new Breaks()
var a = 0
loop.breakable {
  for(a <- 0 to 20) {
    println(s"Value of a = $a")
    if(a > 5)
      loop.break()
  }
}
println("Outisde the loop")

Which when run gives the following results:

image

This article was originally posted at https://sachabarbs.wordpress.com/2015/10/15/scala-loops
This article is part of the series 'Scala View All

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
-- There are no messages in this forum --