1.流程控制
1)do..while
1 def doWhile(){2 var line=""3 do{4 line = readLine()5 println("readlineConetent" + line )6 }while(line != " ")7 }
2)for
方式一:
1 for (arg <-args)2 println(arg)
方式二:
1 for (i <- 1 to 10 if i % 2 == 0) 2 println(i)
for参数里面同样支持if判断。比如输出1——10中的偶数
1 for (i <- 1 to 10 if i % 2 == 0) 2 println(i)
增强for循环
1 //增强for循环2 def doForStrenth(){3var files = (new java.io.File(".")).listFiles()4for(file <- files){5 println(file)6}7 }
3)while
1 var i=02 while (i < args.length) {3 println (args(i))4 i+=15 }
4)if .. else .. (可以将结果直接赋值给一个变量)
1 var file = if(args.isEmpty) args(0) else "spark"2 println(file)
注意:
1)Scala不支持i++和++i,需要使用i+=1来实现加一;
2.异常处理
try .. catch 机制,
1 //异常部分 2def doException(){ 3 val n = 99 4 val file = "scala.text" 5// OpenFileInputStreamAction 6 try { 7 val half = if (n % 2 == 0) n/2 else throw new RuntimeException("N must be event") 8 } catch { 9 case e: Exception => println("This exception is :" +e.getMessage() )10 } finally {11 // close(file)12 }13