与 while 循环在循环顶部测试循环条件不同, do-while 循环在循环底部检查其条件, do-while 循环与while循环相似,除了保证do-while循环至少执行一次

do-while - 语法

以下是do-while循环的语法。

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

do-while - 流程图

Scala do...while loop

尝试使用以下示例程序来了解Scala编程语言中的循环控制语句(while语句)。

do-while - 示例

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

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

将上述程序保存在 Demo.scala 中。以下命令用于编译和执行该程序。

\>scalac Demo.scala
\>scala Demo
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

参考链接

https://www.learnfk.com/scala/scala-do-while-loop.html