Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, you have multiple loop constructs available. Let's cover the most common ones:
while
Loop:The while
loop repeatedly executes a block of code as long as a given condition is true
.
var count = 0 while (count < 5) { println(count) count += 1 }
do..while
Loop:In the do..while
loop, the block of code is executed once before the condition is checked, and then it continues to execute as long as the condition is true
.
var count = 0 do { println(count) count += 1 } while (count < 5)
for
Loop:Scala's for
loop is more flexible than traditional ones. It allows for iterating over ranges, collections, and even provides comprehension features.
Range Iteration:
for (i <- 1 to 5) { println(i) }
Collection Iteration:
val fruits = List("apple", "banana", "cherry") for (fruit <- fruits) { println(fruit) }
Guard Conditions:
for (i <- 1 to 10 if i % 2 == 0) { // print even numbers only println(i) }
Yielding Values (For Comprehension):
val doubled = for (i <- 1 to 5) yield i * 2 println(doubled) // Vector(2, 4, 6, 8, 10)
You can nest any combination of loops within one another.
for (i <- 1 to 3) { for (j <- 1 to 3) { println(s"i = $i, j = $j") } }
Or with a single for
loop:
for { i <- 1 to 3 j <- 1 to 3 } println(s"i = $i, j = $j")
While loops are familiar to developers from other languages, idiomatic Scala code often avoids traditional looping constructs in favor of higher-order functions like map
, filter
, foreach
, and others. These not only make the code more concise but also more expressive and functional.
However, in performance-critical sections, traditional loops like while
might be more efficient than some higher-order functions, so it's always essential to consider the specific context and trade-offs.
Scala While Loop Example:
The while
loop repeats a block of code while a condition is true.
var i = 0 while (i < 5) { println(s"Iteration $i") i += 1 }
For Loop in Scala with Examples:
The for
loop is versatile and can iterate over collections, ranges, or any sequence.
for (i <- 0 until 5) { println(s"Iteration $i") }
Nested Loops in Scala:
Nest loops to perform complex iterations.
for (i <- 1 to 3) { for (j <- 1 to 2) { println(s"($i, $j)") } }
Loop Control Statements in Scala:
Scala provides break
and continue
alternatives using Breaks
and if
statements.
import scala.util.control.Breaks._ breakable { for (i <- 1 to 5) { if (i == 3) break println(s"Iteration $i") } }
Break and Continue in Scala Loops:
Use Breaks
for break
and if
for continue
.
import scala.util.control.Breaks._ breakable { for (i <- 1 to 5) { if (i == 3) break println(s"Iteration $i") } }
Iterating Over Collections with For Loop in Scala:
Iterate over collections using for
.
val myList = List(1, 2, 3, 4, 5) for (element <- myList) { println(element) }
Using Yield in Scala For Comprehension:
Use yield
in a for
comprehension to create a new collection.
val squaredList = for (i <- 1 to 5) yield i * i
Infinite Loops in Scala:
Create an infinite loop using while (true)
or recursion.
while (true) { // Infinite loop }
Looping Through Ranges in Scala:
Utilize ranges for concise loop expressions.
for (i <- 1 to 5) { println(s"Iteration $i") }