日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Scala中的do ... while循环

發布時間:2023/12/1 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Scala中的do ... while循环 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

做...在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

總結

以上是生活随笔為你收集整理的Scala中的do ... while循环的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。