Latest web development tutorials

Scala do ... while loop

Scala cycle Scala cycle

Unlike the while loop in front of the loop test loop condition, Scala language, do ... while loop is to check its condition at the end of the loop.

do ... while loop is similar to the while loop, but do ... while loop will ensure the implementation of at least a cycle.


grammar

Scala languagewhile loop syntax:

do {
   statement(s);
} while( condition );

flow chart

Scala in the do ... while loop

Please note that the conditional expressions appear at the end of the loop, so the loop statement (s) will be executed at least once before the condition is tested.

If the condition is true, the flow of control jumps back above do, and then re-execute the loop statement (s).

This process is repeated until the given condition becomes false.

Examples

object Test {
   def main(args: Array[String]) {
      // 局部变量
      var a = 10;

      // do 循环
      do{
         println( "Value of a: " + a );
         a = a + 1;
      }while( a < 20 )
   }
}

Execute the above code output results:

$ scalac Test.scala
$ scala Test
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Scala cycle Scala cycle