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, the for
loop is a versatile control structure that allows iteration over a range of values, collections, and even allows for comprehensions with filtering. Let's delve into the various ways you can use the for
loop in Scala:
The simplest form of a for
loop iterates over a range of numbers:
for (i <- 1 to 5) { println(i) // Prints numbers from 1 to 5 } for (i <- 1 until 5) { println(i) // Prints numbers from 1 to 4 }
You can iterate over elements of a collection like a List, Array, Set, etc.:
val fruits = List("apple", "banana", "cherry") for (fruit <- fruits) { println(fruit) }
You can add conditions to your for
loop with the if
keyword:
for (i <- 1 to 10 if i % 2 == 0) { println(i) // Prints even numbers between 1 and 10 }
You can nest multiple for
loops to iterate over multiple collections or ranges:
for { i <- 1 to 3 j <- 1 to 3 } { println(s"i = $i, j = $j") }
The for
loop can also produce values, which is known as a for-comprehension:
val numbers = List(1, 2, 3, 4, 5) val squares = for (n <- numbers) yield n * n println(squares) // List(1, 4, 9, 16, 25)
In a for-comprehension, you can use multiple generators and filters:
val pairs = for { i <- 1 to 3 j <- 1 to 3 if i != j } yield (i, j) println(pairs) // Vector((1,2), (1,3), (2,1), (2,3), (3,1), (3,2))
You can combine pattern matching with for
loops:
val listOfTuples = List((1, "one"), (2, "two"), (3, "three")) for ((number, word) <- listOfTuples) { println(s"Number: $number, Word: $word") }
The for
loop in Scala provides a concise and expressive way to handle iterations and comprehensions. Its versatility in handling different data types, nested iterations, guards, and pattern matching makes it a powerful feature in Scala.
Using for loops for iteration in Scala:
for (i <- 1 to 5) { println(s"Iteration $i") }
Range-based for loops in Scala:
to
or until
methods to define a range of values.for (i <- 1 to 5) { println(s"Value: $i") }
For loop with conditions in Scala:
for (i <- 1 to 10 if i % 2 == 0) { println(s"Even Value: $i") }
Nested for loops in Scala:
for (i <- 1 to 3; j <- 1 to 3) { println(s"($i, $j)") }
Yield keyword in Scala for loops:
yield
keyword transforms a for loop into a generator, creating a new collection.val squares: Seq[Int] = for (i <- 1 to 5) yield i * i
Comprehensions and generators in Scala for loops:
val pairs: Seq[(Int, Int)] = for { i <- 1 to 3 j <- 1 to 2 if i != j } yield (i, j)
For loop vs. foreach in Scala:
val squares: Seq[Int] = for (i <- 1 to 5) yield i * i
val numbers: Seq[Int] = Seq(1, 2, 3, 4, 5) numbers.foreach(n => println(n))
For loop with collections in Scala:
val colors: Seq[String] = Seq("Red", "Green", "Blue") for (color <- colors) { println(s"Color: $color") }
Parallel collections and for loops in Scala:
val numbers: ParSeq[Int] = (1 to 10).par for (n <- numbers) { println(s"Number: $n") }
For loop with yield and map in Scala:
yield
and map
can transform and process collections.val numbers: Seq[Int] = Seq(1, 2, 3, 4, 5) val squaredDoubled: Seq[Int] = for (n <- numbers) yield n * n * 2
Looping over arrays and lists in Scala:
val array: Array[Int] = Array(1, 2, 3, 4, 5) for (element <- array) { println(s"Element: $element") }
val list: List[String] = List("A", "B", "C") for (item <- list) { println(s"Item: $item") }
Common patterns for using for loops in Scala:
val evens: Seq[Int] = for (i <- 1 to 10 if i % 2 == 0) yield i
val squares: Seq[Int] = for (i <- 1 to 5) yield i * i
val colors: Seq[String] = Seq("Red", "Green", "Blue") for (color <- colors) { println(s"Color: $color") }