for循环是一个循环控制结构,可以有效地编写需要执行的特定次数的循环。Scalar的循环说明如下的各种形式:
Scala中for循环最简单的语法是:
for( var x <- Range ){
statement(s);
}
在这里,范围可能是一个数字范围,并且表示为i到j或有时像i到j左箭头< - 操作者被称为生成器,这样命名是因为它是从一个范围产生单个数值。
下面是循环使用范围从i到j语法的例子:
object Test {
def main(args: Array[String]) {
var a = 0;
// for loop execution with a range
for( a <- 1 to 10){
println( "Value of a: " + a );
}
}
}
让我们编译和运行上面的程序,这将产生以下结果:
C:/>scalac Test.scala C:/>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 C:/>
下面是使用循环范围 i到j的语法的例子:
object Test {
def main(args: Array[String]) {
var a = 0;
// for loop execution with a range
for( a <- 1 until 10){
println( "Value of a: " + a );
}
}
}
让我们编译和运行上面的程序,这将产生以下结果:
C:/>scalac Test.scala C:/>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 C:/>
for循环中,循环将遍历给定范围内的所有可能的计算,可以使用分号 (;) 分隔多个范围。下面是使用两个范围的例子,也可以使用两个以上的范围。
object Test {
def main(args: Array[String]) {
var a = 0;
var b = 0;
// for loop execution with a range
for( a <- 1 to 3; b <- 1 to 3){
println( "Value of a: " + a );
println( "Value of b: " + b );
}
}
}
让我们编译和运行上面的程序,这将产生以下结果:
C:/>scalac Test.scala C:/>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 C:/>
for循环使用集合的语法如下:
for( var x <- List ){
statement(s);
}
在这里,List变量是具有元素通过所有在同一时间返回的变量x一个元素的元素的列表和循环迭代集合类型。
以下是 for循环使用数字集合的例子。在这里,我们创建这个集合usingList()。我们将会学习集合在往后单独的一章。
object Test {
def main(args: Array[String]) {
var a = 0;
val numList = List(1,2,3,4,5,6);
// for loop execution with a collection
for( a <- numList ){
println( "Value of a: " + a );
}
}
}
让我们编译和运行上面的程序,这将产生以下结果:
C:/>scalac Test.scala C:/>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 C:/>
Scala for循环允许过滤出使用一个或多个某些元素if语句(多个)。以下是对用于沿使用过滤器循环的语法。
for( var x <- List
if condition1; if condition2...
){
statement(s);
}
要添加多个过滤器到一个for表达式,分离过滤用分号(;)。
以下是for循环使用滤器的例子:
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 execution with multiple filters
for( a <- numList
if a != 3; if a < 8 ){
println( "Value of a: " + a );
}
}
}
让我们编译和运行上面的程序,这将产生以下结果:
C:/>scalac Test.scala C:/>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 C:/>
可以从存储中返回for循环中的变量的值,也可以通过函数返回。要做到这一点,可以通过关键字yield前缀的for表达式体,如下所示:
var retVal = for{ var x <- List
if condition1; if condition2...
}yield x
注意在大括号已被用来保持变量和条件以及retVal的是其中x的所有值将被储存在收集的形式的变量。
以下为例子,说明了for循环及yield的用法:
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 execution with a yield
var retVal = for{ a <- numList
if a != 3; if a < 8
}yield a
// Now print returned values using another loop.
for( a <- retVal){
println( "Value of a: " + a );
}
}
}
让我们编译和运行上面的程序,这将产生以下结果:
C:/>scalac Test.scala C:/>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 C:/>