Scala-基础-流程控制语句
import junit.framework.TestCase import scala.util.control.Breaks._ //流程控制语句 //关键字 if else match class Demo4 extends TestCase { //规则1 if else 有返回值,取最后的值 //规则2 默认返回的值,当值没有设置时,用()代替 //规则3 java中的switch没有出现,被scala中的 match代替了,该match允许在匹配值期间,做相关配合运算 // if else def test_+ { val k1 = 105 val k2 = 101 println(if (k2 > k1) k2); println(if (k2 > k1) 100); println(if (k2 > k1) 100 else 101); val m1 = if (k1 == 105) { if (k2 == 101) 103 else 104 } else if (k2 == 101) { 200; } else { 300; } } //switch def test_() { val k1 = 11; var result = 0; val m1 = k1 match { case 100 => result = 1056; "ABC"; case 101 => result = 1001; "CBA"; case _ => result = 1002; "CAB"; } println(result); println(m1); } //for 循环 def test_*() { //for(i < 1 to n ) println() // for(i <- 1 to 10) print(i+",") var rs = 0 for (i <- 1 to 10) { rs += i } rs = 0; //for循环 倒序 for(i <- (1 to 5).reverse){print(i+" ")} for (i <- 1 to 3; j <- 1 to 3) { rs += (i * j) } for (i <- 1 to 10 if i != 5) { // println(i); } for (i <- 1 to 10 if i != 5; j <- 1 to 10 if j != 8) { //println(i*j); } for (i <- 1 to 10; from = i * 2; j <- 1 to from) { //println(j); } } //while 循环 def test_/() { var rs = 0; var i = 0; while (i < 10) { i += 1; // println(i); rs += i; } //println(rs); rs = 0; i = 10; while (i > 0) { rs += i; i -= 1; } println(rs); } //break;countinue; def test_^() { var flag = true; for (i <- 1 to 10 if flag) { if (i == 5) { flag = false; } if(flag){ //println(i); } flag = true; } breakable{ for(i <- 1 to 5){ if(i==4){ break; } println(i); } } } }