English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
for 循环允许您编写一个执行指定次数的循环控制结构。
Scala 语言中 for 循环的语法:
for( var x <- Range ){ statement(s); }
以上语法中,Range 可以是一个数字区间表示 i to j ,或者 i until j。左箭头 <- 用于为变量 x 赋值。
以下是一个使用了 i to j 语法(包含 j)的示例:
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 above code is as follows:
$ 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
以下是一个使用了 i until j 语法(不包含 j)的示例:
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 above code is as follows:
$ 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
在 for loop 中你可以使用分号 (;) 来设置多个区间,它将迭代给定区间所有的可能值。以下示例演示了两个区间的循环示例:
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 above code is as follows:
$ 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 循环集合的语法如下:
for( x <- List ){ statement(s); }
以上语法中, List 变量是一个集合,for 循环会迭代所有集合的元素。
以下示例将循环数字集合。我们使用 List() 来创建集合。再以后章节我们会详细介绍集合。
object Test {
def main(args: Array[String]) {
var a = 0;
val numList = List(1,2,3,4,5,6);
// for loop
for( a <- numList ){
println( "Value of a: " + a );
}
}
}
The output of the above code is as follows:
$ 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
Scala 可以使用一个或多个 if 语句来过滤一些元素。
以下是在 for 循环中使用过滤器的语法。
for( var x <- List if condition1; if condition2... { statement(s);
You can use a semicolon (;) to add one or more filtering conditions to an expression.
The following is an example of filtering in a for loop:
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 <- numList
if a != 3; if a < 8 ){
println( "Value of a: " + a );
}
}
}
The output of the above code is as follows:
$ 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
You can store the return value of a for loop as a variable. The syntax is as follows:
var retVal = for{ var x <- List if condition1; if condition2... }yield x
Note the curly braces used to store variables and conditionsretVal It is a variable, and the yield in the loop will remember the current element and store it in the collection, returning the collection after the loop ends.
The following example demonstrates the use of yield in a for loop:
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 <- numList
if a != 3; if a < 8
}yield a
// Output the returned value
for( a <- retVal){
println( "Value of a: " + a );
}
}
}
The output of the above code is as follows:
$ 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