8.14. Scala break statement

发布时间 : 2025-10-25 13:35:13 UTC      

Page Views: 10 views

There is no break statement by default in the Scala language, but you can implement it in another way after version 2.8 of Scala. break statement. When used in a loop break statement, when executed, breaks the loop and executes the block of code after the body of the loop.

8.14.1. Grammar #

In Scala the break syntax is a little different, and the format is as follows:

//Import the following package Import scala. util. control_ //Create Breaks object Val loop=new Breaks; //Loop in breakable Loop. breakable{ //Loop For (...){ //Loop interrupt Loop. break; } } 

8.14.2. Flow chart #

Image0

Example #

import scala.util.control._ object Test { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6,7,8,9,10); val loop = new Breaks; loop.breakable { for( a <- < span> numList){ println( "Value of a: " + a ); if( a == 4 ){ loop.break; } } } println( "After the loop" ); } }    

The output result of executing the above code is:

$ scalac Test.scala $ scala Test Value of a: 1 Value of a: 2 Value of a: 3 Value of a: 4 After the loop 

8.14.3. Interrupt a nested loop #

The following example shows how to break a nested loop:

import scala.util.control._ object Test { def main(args: Array[String]) { var a = 0; var b = 0; val numList1 = List(1,2,3,4,5); val numList2 = List(11,12,13); val outer = new Breaks; val inner = new Breaks; outer.breakable { for( a <- < span> numList1){ println( "Value of a: " + a ); inner.breakable { for( b <- < span> numList2){ println( "Value of b: " + b ); if( b == 12 ){ inner.break; } } } // Embedded loop interrupt } } // External loop interrupt } }    

The output result of executing the above code is:

$ scalac Test.scala $ scala Test Value of a: 1 Value of b: 11 Value of b: 12 Value of a: 2 Value of b: 11 Value of b: 12 Value of a: 3 Value of b: 11 Value of b: 12 Value of a: 4 Value of b: 11 Value of b: 12 Value of a: 5 Value of b: 11 Value of b: 12 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。