learning scala control statement
Posted lianghong881018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了learning scala control statement相关的知识,希望对你有一定的参考价值。
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)
以上是关于learning scala control statement的主要内容,如果未能解决你的问题,请参考以下文章
learning scala for comprehensions
learning scala pattern matching 03
learning scala dependency injection
lec-1-Deep Reinforcement Learning, Decision Making, and Control