Hello and welcome to today's journey into loops in Scala! Imagine if you've been assigned the task of counting the trees in your area. Would you do it individually? That wouldn't be efficient, right? That's where loops come to the rescue — saving time and effort by automating repetitive tasks. Scala offers a type of loop known as the for
loop, which is particularly useful when the number of repetitions is known.
We'll start with the for
loop. It offers an orderly and efficient way of accomplishing repetitive tasks in Scala. If you possess a list of names and wish to print them, you can employ the for
loop:
Scala1val names = List("John", "Sarah", "Jane", "Tom") 2 3// Our 'for' loop 4for (name <- names) { 5 println(name) // Prints each name in the list 6}
In every iteration of the loop, a new name
is selected from names
and then printed.
Are you prepared to delve deeper into for
loops? Scala provides the flexibility to denote ranges that define start and end values. For instance, to print numbers from 1 to 5, you can use:
Scala1// Loop from 1 to 5 and print each number 2for (i <- 1 to 5) { 3 println(i) // Outputs numbers from 1 through 5 4}
To perform a loop in reverse order, we can define a range with steps of -1
:
Scala1// Loop from 5 to 1 2for (i <- 5 to 1 by -1) { 3 println(i) // Outputs numbers from 5 to 1 in descending order 4}
To access the index along with the value in a loop, you can use the zipWithIndex
method:
Scala1val names = List("John", "Sarah", "Jane", "Tom") 2 3// Loop through the list with zipWithIndex 4for ((value, index) <- names.zipWithIndex) { 5 println(s"The element at $index is $value") // Prints index and corresponding name 6}
Alternatively, you can use the .indices
method to loop through the indices directly:
Scala1val names = List("John", "Sarah", "Jane", "Tom") 2 3// Loop through the list using .indices 4for (i <- names.indices) { 5 println(s"The element at $i is ${names(i)}") // Prints index and corresponding name 6}
Remember these tips as they can enhance your coding efficiency:
For example:
Scala1// Loop over a range of numbers 2for (i <- 1 to 3) { 3 val innerValue = "hello" // Declare a variable in the loop 4} 5println(innerValue) // Error! innerValue is out of the scope
Our innerValue
is scoped inside the loop, so trying to print it outside will result in an error.
In Scala, single-statement iterations of loops can be written in a concise form without the use of braces ({}
). Here's an example:
Scala1// Correct usage without braces for single-statement iterations 2for (i <- 1 to 3) println(i) // Directly prints each iteration 3 4// Incorrect usage without braces for multi-statement iterations - causes an error 5// for (i <- 1 to 3) println(i); println(s"Loop iteration: $i") // Error: Braces are required for multi-statement iterations 6 7// Correct usage with braces for multi-statement iterations 8for (i <- 1 to 3) { 9 println(s"Loop iteration: $i") // Prints each iteration 10 println(s"Iteration value: $i") // Prints the value of iteration 11}
Scala simplifies the process of creating value ranges, offering an intuitive syntax for defining sequences of numbers:
Scala1val range1 = 1 to 4 // 1, 2, 3, 4 2val range2 = 1 until 4 // 1, 2, 3 3val range3 = 4 to 1 by -1 // 4, 3, 2, 1 4val range4 = 0 to 8 by 2 // 0, 2, 4, 6, 8 5val range5 = 8 to 0 by -2 // 8, 6, 4, 2, 0
Congratulations! You are now familiar with for
loops in Scala. The upcoming practice sessions will help you hone your for
loop skills and solidify your understanding. Let's dive in!