This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation.
Chinese: # Scala do...while loop
Unlike the while loop, which tests the loop condition at the head of the loop, the do...while loop in Scala 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
The syntax for the while loop in Scala is:
do {
statement(s);
} while( condition );
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 at the top, and then re-execute the statement(s) in the loop.
This process will continue to repeat until the given condition becomes false.
Example
Example
object Test {
def main(args: Array[String]) {
// Local variable
var a = 10;
// do loop
do{
println( "Value of a: " + a );
a = a + 1;
}while( a < 20 )
}
}
The output of 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