import scala.util.control.Breaks._
object ForLoop {
def main(args: Array[String]): Unit = {
for (i <- 1 to 10) {
println(i + " hello word")
}
println("=====================================")
for (i <- 1.to(10)) {
println(i + " hello word")
}
println("=====================================")
for (i <- Range(1, 10)) {
println("通过Range,输出的结果不包含10!" + i)
}
println("=====================================")
for (i <- 1 until 10) {
println("通过until,输出的结果不包含10!" + i)
}
println("=====================================")
for (i <- Array(1, 2, 3)) {
println(i + " 集合")
}
println("=====================================")
for (i <- 1 to 10 if (i % 2 == 0)) {
println(i + " 循环守卫")
}
println("=====================================")
for (i <- 1 to 10 by 2) {
println(i + " 循环步长")
}
println("=====================================")
val ints = for (i <- 1 to 10) yield i * 2
println(ints)
println("=====================================")
var a = 10
while (a >= 1) {
println(a + " while loop")
a -= 1
}
println("=====================================")
var b = 10
do {
println(b + " do while")
b -= 1
} while (b >= 1)
println("=====================================")
breakable {
for (i <- 1 to 10) {
println(i + " break")
if (i == 5) {
break()
}
}
}
println("=====================================")
for (i <- 1 to 10) {
breakable {
if (i == 5) {
break()
}
println(i + " continue")
}
}
}
}