Latest web development tutorials

Scala do ... while loop

Scala cycle Scala cycle

for loop allows you to write a specified number of loop control structure.


grammar

Scala languagefor loop syntax:

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

The abovesyntax, Range could be a range of numbers represents i to j,ori until j.Left Arrow <- for assignment to a variable x.

Examples

The following is a use of thei to j syntax (containing j) Examples:

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

Execute the above code output results:

$ 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 a use of thei until j syntax (does not include j) Examples:

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

Execute the above code output results:

$ 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 thefor loop you can use a semicolon (;) to set up a plurality of sections, it will iteration interval given all possible values.The following example demonstrates two examples of cycle interval:

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

Execute the above code output results:

$ 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

for cycling set

The syntax for cycling set as follows:

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

The abovesyntax, List is a collection of variables, for loop iterates over all elements of the collection.

Examples

The following examples will loop digital collection. We useList ()to create the collection. And then we will detail later chapters set.

object Test {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6);

      // for 循环
      for( a <- numList ){
         println( "Value of a: " + a );
      }
   }
}

Execute the above code output results:

$ 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

for loop filter

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

The following is the use of a filter in a for loop syntax.

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

You can use a semicolon (;) to add to the expression of one or more filters.

Examples

The following is a loop filter for instance:

object Test {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6,7,8,9,10);

      // for 循环
      for( a <- numList
           if a != 3; if a < 8 ){
         println( "Value of a: " + a );
      }
   }
}

Execute the above code output results:

$ 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

for use yield

You can return the value for the cycle as a variable storage. Syntax is as follows:

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

Note that curly brackets used to hold variables andconditions,retVal is variable, the loop will yield the current elements down, stored in a collection, the collection returned after the end of the cycle.

Examples

The following example demonstrates the use of the for loop yield:

object Test {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6,7,8,9,10);

      // for 循环
      var retVal = for{ a <- numList 
                        if a != 3; if a < 8
                      }yield a

      // 输出返回值
      for( a <- retVal){
         println( "Value of a: " + a );
      }
   }
}

Execute the above code output results:

$ 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

Scala cycle Scala cycle