Understanding Loop Structures

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.

Introduction to 'For' Loop in Scala

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:

val names = List("John", "Sarah", "Jane", "Tom")

// Our 'for' loop
for (name <- names) {
    println(name)  // Prints each name in the list
}

In every iteration of the loop, a new name is selected from names and then printed.

Variations of 'For' Loop in Scala

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:

// Loop from 1 to 5 and print each number
for (i <- 1 to 5) {
    println(i)  // Outputs numbers from 1 through 5
}

To perform a loop in reverse order, we can define a range with steps of -1:

// Loop from 5 to 1
for (i <- 5 to 1 by -1) {
    println(i)  // Outputs numbers from 5 to 1 in descending order
}
Accessing Index in 'For' Loop
Useful Tips for Looping

Remember these tips as they can enhance your coding efficiency:

  • Avoid altering the collection over which you're looping, within the loop itself.
  • Variables declared within a loop only exist (or in programmer lingo, are "scoped") within the loop.

For example:

// Loop over a range of numbers
for (i <- 1 to 3) {
    val innerValue = "hello"  // Declare a variable in the loop
}
println(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.

Concise For-loop Syntax

In Scala, single-statement iterations of loops can be written in a concise form without the use of braces ({}). Here's an example:

// Correct usage without braces for single-statement iterations
for (i <- 1 to 3) println(i)  // Directly prints each iteration

// Incorrect usage without braces for multi-statement iterations - causes an error
// for (i <- 1 to 3) println(i); println(s"Loop iteration: $i")  // Error: Braces are required for multi-statement iterations

// Correct usage with braces for multi-statement iterations
for (i <- 1 to 3) {
    println(s"Loop iteration: $i")  // Prints each iteration
    println(s"Iteration value: $i")  // Prints the value of iteration
}
Cheat Sheet for Scala Ranges

Scala simplifies the process of creating value ranges, offering an intuitive syntax for defining sequences of numbers:

val range1 = 1 to 4         // 1, 2, 3, 4
val range2 = 1 until 4      // 1, 2, 3
val range3 = 4 to 1 by -1   // 4, 3, 2, 1
val range4 = 0 to 8 by 2    // 0, 2, 4, 6, 8
val range5 = 8 to 0 by -2   // 8, 6, 4, 2, 0
Lesson Summary and Upcoming Practice

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal