In Scala, for loop is also known as for-comprehensions. A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line.
Syntax:
for(w <- range){
// Code..
}
Here, w is a variable, <- operator is known as a generator, according to the name this operator is used to generate individual values from the range, and the range is the value which holds starting and ending values. The range can be represented by using either i to j or i until j.
for loop using to
In for loop, We can use to when We want to print the values from 0 to n. Or in other words, when We use to with for loop it includes both start and end value like as shown in the below program, it prints from 0 to 10 not print from 0 to 9 like in until.
Example:
Scala
// Scala program to illustrate how to
// create for loop using to
object Main
{
def main(args: Array[String])
{
println("The value of w is:");
// Here, the for loop starts from 0
// and ends at 10
for( w <- 0 to 10)
{
println(w);
}
}
}
Output:
The value of w is:
0
1
2
3
4
5
6
7
8
9
10
for loop using until
In for loop, We can use until when We want to print the value from 0 to n-1. Or in other words, until with for loop it excludes the end value like as shown in the below program, it prints only from 0 to 9 not print from 0 to 10.
Example:
Scala
// Scala program to illustrate how to
// create for loop using until
object Main
{
def main(args: Array[String])
{
println("The value of w is:");
// Here, the for loop starts from 0
// and ends at 10
for( w <- 0 until 10)
{
println(w);
}
}
}
Output:
The value of w is:
0
1
2
3
4
5
6
7
8
9
Multiple values in for-loop
We can also use multiple ranges in single for-loop. These ranges are separated by a semi-colon(;). Let us discuss with the help of an example. In the below example, we use two different ranges into a single loop, i.e, w <- 0 to 3; z<- 8 until 10.
Example:
Scala
// Scala program to illustrate how to
// create multiple ranges in for loop
object Main
{
def main(args: Array[String])
{
// for loop with multiple ranges
for( w <- 0 to 3; z<- 8 until 10 )
{
println("Value of w is :" +w);
println("Value of y is :" +z);
}
}
}
Output:
Value of w is :0
Value of y is :8
Value of w is :0
Value of y is :9
Value of w is :1
Value of y is :8
Value of w is :1
Value of y is :9
Value of w is :2
Value of y is :8
Value of w is :2
Value of y is :9
Value of w is :3
Value of y is :8
Value of w is :3
Value of y is :9
Using for-loop with Collections
In Scala, We can use for-loop with collections like List etc. It provides an efficient way to iterate over the collections.
Syntax:
for(i <- List){
// Code..
}
Example:
Scala
// Scala program to illustrate how to
// use for loop with collection
object Main
{
def main(args: Array[String])
{
var rank = 0;
val ranklist = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// For loop with collection
for( rank <- ranklist){
println("Author rank is : " +rank);
}
}
}
Output:
Author rank is : 1
Author rank is : 2
Author rank is : 3
Author rank is : 4
Author rank is : 5
Author rank is : 6
Author rank is : 7
Author rank is : 8
Author rank is : 9
Author rank is : 10
Using for-loop with Filters
In Scala, for-loop allows you to filter some elements from the given collection using one or more if statements in for-loop.
Syntax:
for(i<- List
if condition1; if condition2; if condition3; ...)
{
// code..
}
Example:
Scala
// Scala program to illustrate how to
// use for loop with filters
object Main
{
def main(args: Array[String])
{
var rank = 0;
val ranklist = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// For loop with filters
for( rank <- ranklist
if rank < 7; if rank > 2 )
{
println("Author rank is : " +rank);
}
}
}
Output:
Author rank is : 3
Author rank is : 4
Author rank is : 5
Author rank is : 6
Explanation: In the above example, the for loop use two filters to filter the given collection. These filters eliminate those ranks which are less than 7 and greater than 2.
Using for-loop with Yield
In Scala, the return value of the for loop is stored in a variable or may return through a function. To do this you should use yield keyword to prefix the body of for loop.
Syntax:
var output = for{ i<- List
if condition 1; if condition 2;
}
yield i
Example:
Scala
// Scala program to illustrate how to
// use for loop with yields
object Main
{
def main(args: Array[String])
{
var rank = 0;
val ranklist = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// For loop with yields
var output = for{ rank <- ranklist
if rank > 4; if rank != 8 }
yield rank
// Display result
for (rank <- output)
{
println("Author rank is : " + rank);
}
}
}
Output:
Author rank is : 5
Author rank is : 6
Author rank is : 7
Author rank is : 9
Author rank is : 10
Explanation: In the above example, the output is a variable where all the values of rank are stored in the form of a collection. And the for loop displays only those Author's rank whose rank is greater than 4 and not equal to rank 8.
Similar Reads
Operators in Scala
An operator is a symbol that represents an operation to be performed with one or more operand. Operators are the foundation of any programming language. Operators allow us to perform different kinds of operations on operands. There are different types of operators used in Scala as follows: Arithmeti
11 min read
while and do while Loop in Scala
Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Loops make the programmers task simpler. Scala provides the different types of loops but in this article we understand while and do-whil
4 min read
Scala Long ==(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The ==(x: Int) method is utilized to return true if this value is equal to x, false otherwise. Method Definition - def ==(x: Int): Boolean Returns - Returns true if this value is equal to than x, false othe
1 min read
Scala Long >=(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Int) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Int): Boolean Returns - Returns true if this value is great
1 min read
Scala Long >(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >(x: Int) method is utilized to return true if this value is greater than x, false otherwise. Method Definition - def >(x: Int): Boolean Returns - Returns true if this value is greater than x, fal
1 min read
Scala Long *(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The *(x: Long) method is utilized to return the product of the specified Long value and Int value. Method Definition - def *(x: Int) Returns - Returns the product of this value and x. Example #1: SCALA // S
1 min read
Scala Long ==(x: Long) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The ==(x: Long) method is utilized to return true if this value is equal to x, false otherwise. Method Definition - def ==(x: Long): Boolean Returns - Returns true if this value is equal to x, false otherwi
1 min read
Scala Long >=(x: Float) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Float) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Float): Boolean Returns - Returns true if this value is g
1 min read
Lambda Expression in Scala
Lambda Expression refers to an expression that uses an anonymous function instead of variable or value. Lambda expressions are more convenient when we have a simple function to be used in one place. These expressions are faster and more expressive than defining a whole function. We can make our lamb
4 min read
Scala Long >=(x: Char) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Char) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Char): Boolean Returns - Returns true if this value is gre
1 min read