Scala中的continue和break,即breakable和break

在Scala中要想实现对循环的控制,即continue和break.
Scala中有一个Breaks类(在包scala.util.control中)
  • break
// D:\Scala\test\Breaks.scala
import scala.util.control.Breaks._
val array = Array(1,3,10,5,4)
breakable{
for (i<-array){
    if(i>5) break//跳出breakable,终止for循环
println(i)
    }
}

D:\Scala\test>scala Breaks.scala
1
3
  • continue
// D:\Scala\test\Breaks1.scala
import scala.util.control.Breaks._
val array = Array(1,3,10,5,4)
for (i<-array){
    breakable{
        if(i>5)break
println(i)
        }
}

D:\Scala\test>scala Breaks1.scala
1
3
5
4

你可能感兴趣的:(Scala中的continue和break,即breakable和break)