做...在Scala循環 (do...while loop in Scala)
do...while loop in Scala is used to run a block of code multiple numbers of time. The number of executions is defined by an exit condition. If this condition is TRUE the code will run otherwise it runs the first time only
Scala中的do ... while循環用于多次運行代碼塊。 執行次數由退出條件定義。 如果此條件為TRUE,則代碼將運行,否則它將僅在第一次運行
The do...while loop is used when the program does not have information about the exact number of executions taking place. The number of executions is defined by an exit condition that can be any variable or expression, the value evaluated in TRUE if it's positive and FALSE if it's zero.
當程序沒有有關發生的確切執行次數的信息時,使用do ... while循環 。 執行次數由退出條件定義,退出條件可以是任何變量或表達式,如果值為正數則為TRUE,如果為零則為FALSE 。
This loop always runs once in the life span of code. If the condition is initially FALSE. The loop will run once in this case.
該循環在代碼的生命周期中始終運行一次。 如果條件最初為FALSE 。 在這種情況下,循環將運行一次。
The do...while loop is also called exit controlled loop because its condition is checked after the execution of the loop's code block.
do ... while循環也稱為退出控制循環,因為在執行循環的代碼塊后會檢查其條件。
Syntax of do...while loop:
do ... while循環的語法:
do{
//Code to be executed...
}
while(condition);
Flow chart of do...while loop:
do ... while循環流程圖:

Example of do...while loop:
do ... while循環的示例:
object MyClass {
def main(args: Array[String]) {
var myVar = 12;
println("This code prints myVar even if it is greater that 10")
do{
println(myVar)
myVar += 2;
}
while(myVar <= 10)
}
}
Output
輸出量
This code prints myVar even if it is greater that 10
12
Code explanation:
代碼說明:
This code implements the use of the do...while loop in Scala. The do...while loop being an exit control loop checks the condition after the first run. This is why the code prints 12 but the condition is myVar should not be greater than 10. In this we put the condition after the code block this means the code will run like this, print myVar increment it by 2 (using assignment operator) and then checks for the condition of the loop.
這段代碼實現了Scala中do ... while循環的使用。 作為退出控制循環的do ... while循環在第一次運行后檢查條件。 這就是為什么代碼打印12但條件為myVar不應大于10的原因。在此,我們將條件放在代碼塊之后,這意味著代碼將像這樣運行,打印myVar將其遞增2(使用賦值運算符 ),然后檢查循環條件。
The assignments for the do...while loop that you can complete and submit to know your progress.
您可以完成并提交do ... while循環的作業,以了解自己的進度。
Assignment 1 (difficulty - beginner): Print all prime numbers from 342 - 422 that is divisible by 3. (use do-while loop and functions.)
作業1(難度-初學者):打印342-422中所有可被3整除的質數。(使用do-while循環和函數。)
Assignment 2 (difficulty - intermediate): Print all number between 54 - 1145 that have 11,13 and 17 as a factor.
作業2(難度-中間):打印54-1145之間的所有數字,其中11、13和17為因數。
翻譯自: https://www.includehelp.com/scala/the-do-while-loop-in-scala.aspx