Scala for Loop
The for loop allows you to write a loop control structure that executes a specified number of times.
Syntax
In the Scala language, for The syntax of the do...while loop:
for( var x <- Range ){
statement(s);
}
In the above syntax,Range can be a numeric range expressed as i to j , or i until j1 to 10. The left arrow <<- is used to assign a value to the variable x.
Example
The following is an example using i to j syntax (with j):
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
var
a
=
0
;
// for loop
for
(
a
<
1
to
10
)
{
println
("Value of a: "
- a
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
value of a: 6
value of a: 7
value of a: 8
value of a: 9
value of a: 10
The following is an example using i until j an example of syntax (without j):
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
var
a
=
0
;
// for loop
for
(
a
<
1
until
10
)
{
println
("Value of a: "
- a
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
value of a: 6
value of a: 7
value of a: 8
value of a: 9
in for loop In it you can use a semicolon (;) to set multiple ranges, which iterates over all possible values of the given ranges. The following example demonstrates looping over two ranges:
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
var
a
=
0
;
var
b
=
0
;
// for loop
for
(
a
<
1
to
3;
b
<
1
to
3
)
{
println
("Value of a: "
- a
)
;
println
(
"Value of b: "
- b
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
Value of a: 1
Value of b: 1
Value of a: 1
Value of b: 2
Value of a: 1
Value of b: 3
Value of a: 2
Value of b: 1
Value of a: 2
Value of b: 2
Value of a: 2
Value of b: 3
Value of a: 3
Value of b: 1
Value of a: 3
Value of b: 2
Value of a: 3
Value of b: 3
for Loop Collections
The syntax of a for loop over a collection is as follows:
for( x <- List ){
statement(s);
}
In the above syntax, List The variable is a collection, and the for loop iterates over all elements of the collection.
Example
The following example loops over a collection of numbers. We use List() to create the collection. We will introduce collections in detail in later chapters.
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
var
a
=
0
;
val
numList
=
List
(
1
,
2
,
3
,
4
,
5
,
6
)
;
// for loop
for
(
a
<
)
{
println
("Value of a: "
- a
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
value of a: 6
for Loop Filtering
Scala can use one or more if statements to filter certain elements.
The following is the syntax for using a filter in a for loop.
for( var x <- List
if condition1; if condition2...
){
statement(s);
}
You can use a semicolon (;) to add one or more filter conditions to an expression.
Example
The following is an example of filtering in a for loop:
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
var
a
=
0
;
val
numList
=
List
(
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
)
;
// for loop
for
(
a
<
if
a
!=
3;
if
a
<
8
)
{
println
("Value of a: "
- a
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 4
value of a: 5
value of a: 6
value of a: 7
for with yield
You can store the return value of a for loop in a variable. The syntax is as follows:
var retVal = for{ var x <- List
if condition1; if condition2...
}yield x
Note that inside the braces, variables and conditions are stored,retVal is a variable; the yield in the loop records the current element and saves it in a collection, which is returned when the loop ends.
Example
The following example demonstrates the use of yield in a for loop:
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
var
a
=
0
;
val
numList
=
List
(
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
)
;
// for loop
var
retVal
=
for
{
a
<
if
a
!=
3;
if
a
<
8
}
yield
a// Output the return value
for
(
a
<
)
{
println
("Value of a: "
- a
)
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
value of a: 1
value of a: 2
value of a: 4
value of a: 5
value of a: 6
value of a: 7
Scala Loops