在给定条件为真时重复一个语句或一组语句,它在执行循环体之前判断条件,只要给定条件为真, while 循环语句就会重复执行目标语句。
以下是while循环的语法。
链接:https://www.learnfk.comhttps://www.learnfk.com/scala/scala-while-loop.html
来源:LearnFk无涯教程网
while(condition){ statement(s); }
object Demo { def main(args: Array[String]) { //局部变量声明: var a=10; //while循环执行 while( a < 20 ){ println( "Value of a: " + a ); a=a + 1; } } }
将上述程序保存在 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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)