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

Scala while loop

Scala Loops

As long as the given condition is true, the loop statements in Scala language while Loop statements will repeatedly execute the code block within the loop.

Syntax

In Scala language while The syntax of the loop:

while(condition)
{
   statement(s);
}

Here,statement(s) It can be a single statement or a code block consisting of several statements.

condition It can be any expression, and it will be true when it is any non-zero value. The loop will be executed when the condition is true. When the condition is false, the loop will exit and the program flow will continue to execute the next statement following the loop.

Flowchart

Here,while The key point of the loop is that the loop may not execute even once. When the condition is false, the loop body will be skipped and the next statement following the while loop will be executed directly.

Online Example

object Test {
   def main(args: Array[String]) {
      // Local Variable
      var a = 10;
      // while loop execution
      while( a < 20 ){
         println( "Value of a: " + a );
         a = a + 1;
      }
   }
}

The output of executing the above code is:

$ 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 Loops