Latest web development tutorials

Scala while loop

Scala while loop

Scala cycle Scala cycle

As long as the given condition is true, Scala languagewhile loop repeats the loop body block.


grammar

Scala languagewhile loop syntax:

while(condition)
{
   statement(s);
}

Here, statement (s) may be a single statement, it can also be a block composed of a few statements.condition can be any expression, when an arbitrary non-zero values are true. Execution of the loop when the condition is true.

When the condition is false, program flow will continue with the next statement followed the loop.


flow chart

Scala while loop

Here, the key pointswhileloop is a loop may not execute. When the condition is false, the loop body will skip directly to the next statement immediately while loop.

Examples

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

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

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