1 .if satement
与其它语言不同的是,scala if statement 返回的是一个值
scala> val a = if ( 6 > 0 ) 1 else -1
a: Int = 1
2. while statement
3. do.. while statement
4 for statement
for ( 变量 <- 表达式 ) { 语句块};
scala> for ( i <- 1 to 3 ) println(i)
1
2
3
scala> for ( i <- Array(3,4,5) ) println(i)
3
4
5
scala> for ( i <- 1 to 5 if i%2 == 0 ) println(i)
2
4
scala> for ( i <- 2 to 4; j <- 4 to 6 ) println( i * j)
8
10
12
12
15
18
16
20
24
yield 后的语句块中最后一个表达式的值,作为每次循环的返回值,如下所示:
scala> var fortest = for ( i <- 2 to 4; j <- 4 to 6 ) yield println( i * j)
8
10
12
12
15
18
16
20
24
fortest: scala.collection.immutable.IndexedSeq[Unit] = Vector((), (), (), (), (), (), (), (), ())
scala> var fortest = for ( i <- 2 to 4; j <- 4 to 6 ) yield { println( i * j); i*j }
8
10
12
12
15
18
16
20
24
fortest: scala.collection.immutable.IndexedSeq[Int] = Vector(8, 10, 12, 12, 15, 18, 16, 20, 24)
原文地址:https://www.cnblogs.com/lianghong881018/p/10339095.html