原创转载请注明出处:http://agilestyle.iteye.com/blog/2333012
package org.fool.scala.basic object ScalaFor extends App { var result = "" for (i <- 0 to 9) result += i + " " // 0 1 2 3 4 5 6 7 8 9 println(result) result = "" for (i <- 0 until 10) result += i + " " // 0 1 2 3 4 5 6 7 8 9 println(result) result = "" for (i <- Range(0, 10)) result += i + " " // 0 1 2 3 4 5 6 7 8 9 println(result) result = "" for (i <- Range(0, 20, 2)) result += i + " " // 0 2 4 6 8 10 12 14 16 18 println(result) var sum = 0 for(i <- Range(0, 20, 2)) { println("adding " + i + " to " + sum) sum += i } println("sum is " + sum) }
Console Output