8.13. Scala for cycle

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

Page Views: 10 views

A for loop allows you to write a loop control structure that executes a specified number of times.

8.13.1. Grammar #

In Scala language for the syntax of the loop:

for( var x <- < span> Range ){ statement(s); }    

In the above grammar Range can be a number interval representation. i to j , or i until j . The left arrow <-is < assign p thevariable to used values x.>

8.13.2. Example #

The following is an example that uses the i to j an example of the syntax (including j):

Example #

object Test { def main(args: Array[String]) { var a = 0; // for loop for( a <- < span> 1 to 10){ println( "Value of a: " + a ); } } }    

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 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 value of a: 10 

The following is an example that uses the i until j an example of the syntax (excluding j):

Example #

object Test { def main(args: Array[String]) { var a = 0; // for loop for( a <- < span> 1 until 10){ println( "Value of a: " + a ); } } }    

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 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 

In for the loop you can use the semicolon (;) to set multiple intervals, which will iterate over all possible values for a given interval.The following example shows an example of a loop in two intervals:

Example #

object Test { def main(args: Array[String]) { var a = 0; var b = 0; // for loop for( a <- < span> 1 to 3; b <- < span> 1 to 3){ println( "Value of a: " + a ); println( "Value of b: " + b ); } } }    

The output result of executing the above code is:

$ scalac Test.scala $ scala Test Value of a: 1 Value of b: 1 Value of a: 1 Value of b: 2 Value of a: 1 Value of b: 3 Value of a: 2 Value of b: 1 Value of a: 2 Value of b: 2 Value of a: 2 Value of b: 3 Value of a: 3 Value of b: 1 Value of a: 3 Value of b: 2 Value of a: 3 Value of b: 3 

8.13.3. for Cyclic set #

The for syntax for a loop collection is as follows:

for( x <- < span> List ){ statement(s); }    

In the above grammar List a variable is a collection, and the for loop iterates over the elements of all collections.

8.13.4. Example #

The following example loops a collection of numbers. We use List() to create a collection. We will describe the collection in detail in later chapters.

Example #

object Test { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6); // for loop for( a <- < span> numList ){ println( "Value of a: " + a ); } } }    

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 value of a: 5 value of a: 6 

8.13.5. for Cyclic filtration #

Scala can use one or more if statement to filter some elements.

The following is in for the syntax for using filters in a loop.

for( var x <- < span> List if condition1; if condition2... ){ statement(s); }    

You can use the semicolon (;) to add one or more filter criteria to the expression.

8.13.6. Example #

The following is for instances filtered in the loop:

Example #

object Test { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6,7,8,9,10); // for loop for( a <- < span> numList if a != 3; if a < 8 ){ println( "Value of a: " + a ); } } }    

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: 4 value of a: 5 value of a: 6 value of a: 7 

8.13.7. For uses yield #

You can put for the return value of the loop is stored as a variable. The syntax format is as follows:

var retVal = for{ var x <- < span> List if condition1; if condition2... }yield x    

Note that curly braces are used to hold variables and conditions retVal ,it’s a variable, in a loop. yield the current element is written down and saved in the collection, which is returned at the end of the loop.

8.13.8. Example #

The following example demonstrates for use in a loop yield :

Example #

object Test { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6,7,8,9,10); // for loop var retVal = for{ a <- < span> numList if a != 3; if a < 8 }yield a // Output return value for( a <- < span> retVal){ println( "Value of a: " + a ); } } }    

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: 4 value of a: 5 value of a: 6 value of a: 7 
《地理信息系统原理、技术与方法》  97

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