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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scala中循环守卫_Scala中的循环

發(fā)布時間:2025/3/11 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 scala中循环守卫_Scala中的循环 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

scala中循環(huán)守衛(wèi)

Scala中的循環(huán) (Loops in Scala)

In programming, many times a condition comes when we need to execute the same statement or block of code more than one time. It could be difficult to write the same code multiple times, so programing language developers come up with a solution, name a loop statement.

在編程中,很多情況是我們需要多次執(zhí)行同一條語句或代碼塊時出現(xiàn)的。 可能很難多次編寫相同的代碼,因此編程語言開發(fā)人員想出了一個解決方案,命名為loop語句 。

A Loop statement executes the specified block of code multiple times based on some condition.

Loop語句根據(jù)某種條件多次執(zhí)行指定的代碼塊 。

Scala defines three types of loop statements. They are,

Scala定義了三種循環(huán)語句 。 他們是,

  • for Loop

    循環(huán)

  • while Loop

    while循環(huán)

  • do...while Loop

    做...而循環(huán)

  • 1)循環(huán) (1) for Loop)

    A for loop in used when you want to execute a block of code n number of times. The number n is specified explicitly. It is used when the user knows the number of times the loop needs to be executed beforehand. That is before the loop statement the number of executions is specified.

    當(dāng)您要執(zhí)行n次代碼塊時,使用for循環(huán) 。 數(shù)字n是明確指定的。 當(dāng)用戶事先知道循環(huán)需要執(zhí)行的次數(shù)時,將使用此方法。 那就是在循環(huán)語句之前指定執(zhí)行次數(shù)。

    In for loop, a variable is used. This variable is a counter to the number of loops that are executed using them for a statement.

    在for循環(huán)中 ,使用了一個變量。 此變量是一個計數(shù)器,該計數(shù)器針對使用一條語句執(zhí)行的循環(huán)數(shù)。

    Syntax:

    句法:

    for(loop condition){// code to be executed...}

    Example: There are various ways to use for loop we are discussing basic one here.

    示例:有多種用于循環(huán)的方法,我們在這里討論基本方法。

    object MyClass {def add(x:Int, y:Int) = x + y;def main(args: Array[String]) {var i = 0;print("Print numbers from 5 to 10\n")for( i <- 5 to 10){print(i + "\n")}}}

    Output

    輸出量

    Print numbers from 5 to 10 5 6 7 8 9 10

    2)while循環(huán) (2) while Loop)

    A while loop is used when you want to execute a block of code some condition is TRUE. The condition can be any variable, or expression. When evaluated the all positive values are treated as TRUE and 0 is treated as FALSE.

    當(dāng)您要執(zhí)行代碼塊時,使用while循環(huán) ,某些條件為TRUE 。 條件可以是任何變量或表達式。 求值時,所有正值均被視為TRUE,而0被視為FALSE 。

    It checks the condition before entering the loop's block. If the condition is FALSE then the block is not executed. There might be a condition when the block of code does not execute at all. This condition arises when the condition is initially FALSE. There can also be infinite loop execution if the expression never turns FALSE and no internal termination statement is available.

    它在進入循環(huán)塊之前檢查條件。 如果條件為FALSE ,則不執(zhí)行該塊。 當(dāng)代碼塊根本不執(zhí)行時,可能會出現(xiàn)某種情況。 該條件最初為FALSE時出現(xiàn) 。 如果表達式從不變?yōu)镕ALSE并且沒有可用的內(nèi)部終止語句,則還可能存在無限循環(huán)執(zhí)行。

    Syntax:

    句法:

    while(condition){//code to be executed}

    Example:

    例:

    object MyClass {def main(args: Array[String]) {var myVar = 2; println("This code prints 2's table upto 10")while(myVar <= 10){println(myVar)myVar += 2;}}}

    Output

    輸出量

    This code prints 2's table upto 10 2 4 6 8 10

    3)做...而循環(huán) (3) do...while Loop)

    A do...while loop is when you want to execute a block of code until a specific condition is TRUE. The condition can be any variable or expression. This condition when evaluated is TRUE for all positive value and for 0 it is FALSE.

    do ... while循環(huán)是您要執(zhí)行代碼塊直到特定條件為TRUE時 。 條件可以是任何變量或表達式。 對所有正值求值時,此條件為TRUE ;對于0 ,則為FALSE 。

    It checks the condition before exiting the loop's block. If the condition is FALSE then the block is executed only once. There is never a condition when the block of code does not execute at all. There can be loop execution if the expression never turns FALSE and no internal termination statement is done.

    它在退出循環(huán)塊之前檢查條件。 如果條件為FALSE,則該塊僅執(zhí)行一次。 當(dāng)代碼塊根本不執(zhí)行時,永遠不會有條件。 如果表達式從不變?yōu)镕ALSE并且沒有完成內(nèi)部終止語句,則可能存在循環(huán)執(zhí)行。

    Syntax:

    句法:

    do{//code to be executed}while(condition);

    Example:

    例:

    object MyClass {def main(args: Array[String]) {var myVar = 2; println("This code prints 2's table upto 10")while(myVar <= 10){println(myVar)myVar += 2;}}}

    Output

    輸出量

    This code prints 2's table upto 10 2 4 6 8 10

    翻譯自: https://www.includehelp.com/scala/loops-in-scala.aspx

    scala中循環(huán)守衛(wèi)

    總結(jié)

    以上是生活随笔為你收集整理的scala中循环守卫_Scala中的循环的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。