Scala学习笔记01_条件控制与循环

in cn •  7 years ago 

if表达式

if表达式的定义,if表达式是有值的,就是if或else中最后一行语句返回的值。

scala> val age = 30
age: Int = 30
scala> val isAdult = if(age > 18) 1 else 0
isAdult: Int = 1

if表达式的类型推断,if和else子句的值类型可能不同,Scala会自动进行推断,取两个类型的公共父类型。

scala> if(age>18) "adult" else 0
res3: Any = adult
# if和else的值分别是String和Int,则表达式的值是String和Int的公共父类型Any。

如果if后面没有跟else,则默认else的值是Unit,也用()表示,类似于Java中的void或null。

scala> if(age<12) "children"
res4: Any = ()
# 等价写法
scala> if(age<12) "children" else()
res6: Any = ()

将if语句放在多行中,默认情况下,REPL只能解释一行语句,但是if表达式通常需要放在多行,可以使用{}方式,或者使用:paste和ctrl+D的方式。

scala> if(age < 12) {
     |   "children"} else ()
res7: Any = ()
scala> :paste
// Entering paste mode (ctrl-D to finish)

if (age < 12)
  "children"
else
  "adult"

// Exiting paste mode, now interpreting.

res8: String = adult

语句终结符与块表达式

语句终结符,默认情况下,Scala不需要语句终结符,默认将每一行作为一个语句。如果一行要放多条语句,则必须使用语句终结符分号。通常来说,对于多行语句,还是会使用花括号的方式。

scala> var a,b,c = 0
a: Int = 0
b: Int = 0
c: Int = 0

scala> if (a < 1) {b = b + 1; c = c + 1}

scala> b
res10: Int = 1

scala> c
res11: Int = 1
scala> :paste
// Entering paste mode (ctrl-D to finish)

if(a < 1) {
  b = b + 1
  c = c + 1
}

// Exiting paste mode, now interpreting.

scala> b
res13: Int = 2

scala> c
res14: Int = 2

块表达式,就是{}中的值,其中可以包含多条语句,最后一个语句的值就是块表达式的返回值。

scala> val d = if (a < 1) {b = b + 1; c = c + 1; b + c}
d: AnyVal = 6

输入和输出

print打印时不会加换行符,而println打印时会加一个换行符,printf可以用于进行格式化。readLine允许我们从控制台读取用户输入的数据。

scala> print("Hello World")
Hello World
scala> println("Hello World")
Hello World

scala>
scala> printf("Hello, my name is %s, I'm %d years old", "padluo", 30)
Hello, my name is padluo, I'm 30 years old
scala> readLine()
warning: there was one deprecation warning; re-run with -deprecation for details
res20: String = Hello World

综合案例:游戏厅门禁

scala> val name = readLine("Welcome to Game House. Please show your name:\n")
warning: there was one deprecation warning; re-run with -deprecation for details
Welcome to Game House. Please show your name:
name: String = padluo

scala> println("OK, welcome you, " + name)
OK, welcome you, padluo

scala> println("Then, please show your age:")
Then, please show your age:

scala> val age = readInt()
warning: there was one deprecation warning; re-run with -deprecation for details
age: Int = 30

scala> :paste
// Entering paste mode (ctrl-D to finish)

if(age > 18)
  printf("Hi, %s, you are %d years old, so you are legal to come here!", name, age)
else {
  printf("Sorry, boy, %s, you are only %d years old. you are illegal to come here.")
}

// Exiting paste mode, now interpreting.

Hi, padluo, you are 30 years old, so you are legal to come here!

循环

while do循环

scala> var n = 10
n: Int = 10

scala> :paste
// Entering paste mode (ctrl-D to finish)

while(n > 0) {
  print(n + " ")
  n -= 1
}

// Exiting paste mode, now interpreting.

10 9 8 7 6 5 4 3 2 1

Scala没有for循环,只能使用while替代for循环,或者使用简易版的for语句、或者使用until,也可以对字符串进行遍历,类似Java的增强for循环。

scala> val n = 10
n: Int = 10

scala> for(i <- 1 to n) print(i + " ")
1 2 3 4 5 6 7 8 9 10

scala> for (i <- 1 until n) print(i + " ")
1 2 3 4 5 6 7 8 9
scala> for(c <- "Hello World") print(c + " ")
H e l l o   W o r l d

跳出循环语句,Scala没有类似于Java的break语句。可以使用boolean类型变量、return或者Breaks的break函数来替代使用。

scala> :paste
// Entering paste mode (ctrl-D to finish)

breakable {
  var n = 10
  for(c <- "Hello World") {
    if(n == 5) break;
    print(c)
    n -= 1
  }
}

// Exiting paste mode, now interpreting.

Hello

高级for循环,九九乘法表。

scala> :paste
// Entering paste mode (ctrl-D to finish)

for(i <- 1 to 9; j <- 1 to 9) {
  if (j == 9) {
    println(i * j)
  }
  else {
    print(i * j + " ")
  }
}

// Exiting paste mode, now interpreting.

1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

scala>

if守卫,取偶数。

scala> for(i <- 1 to 20 if i % 2 == 0) print(i + " ")
2 4 6 8 10 12 14 16 18 20

for推导式,构造集合。

scala> for(i <- 1 to 10) yield i
res37: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

本文首发于steem,感谢阅读,转载请注明。

https://steemit.com/@padluo


微信公众号「数据分析」,分享数据科学家的自我修养,既然遇见,不如一起成长。

数据分析


读者交流电报群

https://t.me/sspadluo


知识星球交流群

知识星球读者交流群

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

@padluo, 伦家就觉得你写得不错嘛~~~ img