English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Boucle for Scala

Scala Loops

La boucle for Scala vous permet d'écrire une structure de contrôle de boucle qui exécute un certain nombre de fois.

Syntaxe

Dans le langage Scala for La syntaxe de la boucle :

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

Dans la syntaxe ci-dessus,Range peut être une plage de nombres. i to j ou i until j。Flèche gauche <- Pour affecter une valeur à la variable x.

Online Example

Voici un exemple qui utilise i to j Syntaxe (contient j) exemple :

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

The output of the code above 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

Voici un exemple qui utilise i until j Exemple de syntaxe (ne contient pas j) :

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

The output of the code above 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

Dans for loop Vous pouvez utiliser des points-virgules (;) pour définir plusieurs intervalles, ce qui itérera sur toutes les valeurs possibles de l'intervalle donné. L'exemple suivant montre un exemple de boucle avec deux intervalles :

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

The output of the code above is:

$ scalac Test.scala
$ scala Test
Valeur de a: 1
Valeur de b: 1
Valeur de a: 1
Valeur de b: 2
Valeur de a: 1
Valeur de b: 3
Valeur de a: 2
Valeur de b: 1
Valeur de a: 2
Valeur de b: 2
Valeur de a: 2
Valeur de b: 3
Valeur de a: 3
Valeur de b: 1
Valeur de a: 3
Valeur de b: 2
Valeur de a: 3
Valeur de b: 3

Boucle for sur l'ensemble

La syntaxe de la boucle for sur l'ensemble est la suivante :

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

Dans la syntaxe ci-dessus, List Variable est un ensemble, la boucle for itérera sur tous les éléments de l'ensemble.

Online Example

Dans l'exemple suivant, nous allons itérer sur un ensemble de nombres. Nous utilisons List() Pour créer un ensemble. Nous en parlerons en détail des ensembles dans les chapitres suivants.

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

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

The output of the code above 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

Filtrage par boucle for

Scala peut utiliser une ou plusieurs if Pour filtrer certains éléments.

Le syntaxe utilisée pour le filtre dans la boucle for.

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

You can use semicolons (;) to add one or more filtering conditions to an expression.

Online Example

The following is an example of filtering in a for loop:

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 <- numList
           if a != 3; if a < 8 ){
         println( "Value of a: " + a );
      }
   }
}

The output of the code above 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

for with yield

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

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

Note the curly braces used to save variables and conditionsretVal It is a variable, the yield in the loop will record the current element, save it in the collection, and return the collection after the loop ends.

Online Example

The following example demonstrates the use of yield in for loops:

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 <- numList
                        if a != 3; if a < 8
                      }yield a

      // Output the returned value
      for( a <- retVal){
         println( "Value of a: " + a );
      }
   }
}

The output of the code above 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

Scala Loops