上一章我们共同研究了for循环以及变种。这章我们围绕比较常用的几个其他控制语句学习。

```var range = 0.until(10)
var m = for (p <- range if p > 5) yield p
println(m);```

Vector(6, 7, 8, 9)

```//A simple for-comprehension
for (x <e) yield e’
//is translated to
e.map(x => e’)```

`for (p <persons if p.age > 20) yield p.name`

```class person (name1:String, age1 : Int) {
var  age = age1;
var  name = name1;
}
var persons : List[person] = List(new person("s",1),new person("a",2),new person("b",3));
var n2 = for (p <- persons;if p.age > 1) yield p.name
println(n2);```

`List(a, b)`

`var persons  = Map(a -> 1, b -> 2, c -> 3)`

```var range1 = 1.until(4)
var m1  = Map(1 -> 24, 2 -> 25, 3 -> 26)
var n = for (p <- range1 if p > 1) yield m1(p)
println(n)```

`Vector(25, 26)`

for语句还可以更复杂点

```var m =  for { i <- 0.until(10)
j <- 1.until(i)
if i+j>5} yield (i, j)
println(m)```

```while (true) {
//TODO do something
}```

```do{ //DO SOMETHING}
while (true)```

java程序员可能会犯的错误

```var line = ""
while ((line = readLine()) != "") //记住scala不支持这种方式，line会得不到值

```var a = 1;
var i = 0;
a  match {
case 1 =>  i = 2
case 2 =>  i = 5
}
println(i);```

```def match_re : Int={
1
}
def match_1={
var i = 0;
match_re  match {
case 1 =>  i = 2
println("i = 2");
case 2 =>  i = 5
println("i = 5");
}```

```var a = 3
if(a>3){
println("3 success");
}else if(a>2){
println("2 success");
}else{
println("success");
}```