你可以这样赋值,
val s = if (x > 0) 1 else -1这样就不必像下面这样,这里s就必须是一个变量了。
if (x > 0) s = 1 else s = -1你甚至可以根据情况返回不同类型的值
if (x > 0) "OK" else -1除非多个语句在一行出现,比如
if (n > 0) { r = r * n; n -= 1 }之前已经提到了,可以使用大括号包含一个语句块,从而实现比较复杂的计算然后赋值。
val distance = { val dx = x - x0; val dy = y - y0; sqrt(dx * dx + dy * dy) }简单while
def gcdLoop(x: Long, y: Long): Long = {
    var a = x
    var b = y
    while (a != 0) {
        val temp = a 
        a=b%a
        b = temp
   }
b 
}简单for循环
for (i <- 1 to 4)
       println("Iteration "+ i)加入Filter
 val filesHere = (new java.io.File(".")).listFiles
        for (file <- filesHere if file.getName.endsWith(".scala"))
println(file)多个生成器
def grep(pattern: String) =
          for (
            file <- filesHere
            if file.getName.endsWith(".scala");
            line <- fileLines(file)
            if line.trim.matches(pattern)
          ) println(file +": "+ line.trim)Scala基本沿用Java的异常。但是没有checked exception,捕捉异常可以使用灵活的模式匹配
try {
    process(new URL("http://horstmann.com/fred-tiny.gif"))
} catch {
    case _: MalformedURLException => println("Bad URL: " + url) 
    case ex: IOException => ex.printStackTrace()
}和其他Scala控制结构一样,try-catch-finally是有返回值的。下面的例子教你如何在异常发生时返回一个默认值。注意的时finally通常用来做资源回收,不要依赖finally返回值。
def urlFor(path: String) =
          try {
            new URL(path)
          } catch {
            case e: MalformedURLException =>
              new URL("http://www.scala-lang.org")
}原文:http://tongqingqiu.iteye.com/blog/2189244