Note: This post was created awhile back on another platform (Tumblr) and I migrated to my current one.
while
Loop
1
2
3
4
5
|
var i = 0
while (i < 10) {
println(i)
i += 1
}
|
for
Loop
Using a Range (until
)
1
2
3
|
for (i <- 0 until 10) {
println(i)
}
|
Iterating Over a Collection
1
2
3
|
for (s <- listOfStrings) {
// Do something with s
}
|
foreach
Loop (More Functional Approach)
1
2
3
4
5
|
listOfStrings.foreach(doSomething)
def doSomething(s: String): Unit = {
// Process s
}
|
Reference
Loops in Scala – Matt Hicks