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

if表达式

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

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

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

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

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

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

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

1scala> if(age < 12) {
2     |   "children"} else ()
3res7: Any = ()
1scala> :paste
2// Entering paste mode (ctrl-D to finish)
3if (age < 12)
4  "children"
5else
6  "adult"
7// Exiting paste mode, now interpreting.
8res8: String = adult

语句终结符与块表达式

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

1scala> var a,b,c = 0
2a: Int = 0
3b: Int = 0
4c: Int = 0
5scala> if (a < 1) {b = b + 1; c = c + 1}
6scala> b
7res10: Int = 1
8scala> c
9res11: Int = 1
 1scala> :paste
 2// Entering paste mode (ctrl-D to finish)
 3if(a < 1) {
 4  b = b + 1
 5  c = c + 1
 6}
 7// Exiting paste mode, now interpreting.
 8scala> b
 9res13: Int = 2
10scala> c
11res14: Int = 2

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

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

输入和输出

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

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

综合案例:游戏厅门禁

 1scala> val name = readLine("Welcome to Game House. Please show your name:\n")
 2warning: there was one deprecation warning; re-run with -deprecation for details
 3Welcome to Game House. Please show your name:
 4name: String = padluo
 5scala> println("OK, welcome you, " + name)
 6OK, welcome you, padluo
 7scala> println("Then, please show your age:")
 8Then, please show your age:
 9scala> val age = readInt()
10warning: there was one deprecation warning; re-run with -deprecation for details
11age: Int = 30
12scala> :paste
13// Entering paste mode (ctrl-D to finish)
14if(age > 18)
15  printf("Hi, %s, you are %d years old, so you are legal to come here!", name, age)
16else {
17  printf("Sorry, boy, %s, you are only %d years old. you are illegal to come here.")
18}
19// Exiting paste mode, now interpreting.
20Hi, padluo, you are 30 years old, so you are legal to come here!

循环

while do循环

 1scala> var n = 10
 2n: Int = 10
 3scala> :paste
 4// Entering paste mode (ctrl-D to finish)
 5while(n > 0) {
 6  print(n + " ")
 7  n -= 1
 8}
 9// Exiting paste mode, now interpreting.
1010 9 8 7 6 5 4 3 2 1

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

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

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

 1scala> :paste
 2// Entering paste mode (ctrl-D to finish)
 3breakable {
 4  var n = 10
 5  for(c <- "Hello World") {
 6    if(n == 5) break;
 7    print(c)
 8    n -= 1
 9  }
10}
11// Exiting paste mode, now interpreting.
12Hello

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

 1scala> :paste
 2// Entering paste mode (ctrl-D to finish)
 3for(i <- 1 to 9; j <- 1 to 9) {
 4  if (j == 9) {
 5    println(i * j)
 6  }
 7  else {
 8    print(i * j + " ")
 9  }
10}
11// Exiting paste mode, now interpreting.
121 2 3 4 5 6 7 8 9
132 4 6 8 10 12 14 16 18
143 6 9 12 15 18 21 24 27
154 8 12 16 20 24 28 32 36
165 10 15 20 25 30 35 40 45
176 12 18 24 30 36 42 48 54
187 14 21 28 35 42 49 56 63
198 16 24 32 40 48 56 64 72
209 18 27 36 45 54 63 72 81
21scala>

if守卫,取偶数。

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

for推导式,构造集合。

1scala> for(i <- 1 to 10) yield i
2res37: 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


知识星球交流群

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值