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

Scala do...while loop

Scala Loops

Unlike the while loop, which tests the loop condition at the head of the loop, in Scala language, the do...while loop checks its condition at the end of the loop.

The do...while loop is similar to the while loop, but the do...while loop ensures that the loop is executed at least once.

Syntax

In Scala language, while Loop Syntax:

do {
   statement(s);
}

Flowchart

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

If the condition is true, the control flow will jump back to the do above and then re-execute the statement(s) in the loop.

This process will repeat continuously until the given condition becomes false.

Online Example

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

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