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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ruby循环_Ruby循环

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

ruby循環(huán)

Ruby循環(huán) (Ruby Loops)

Loops are comprised of sequentially group instructions which are executed repeatedly until a certain condition is met. Loops provide great help in making the programmer's task easier.

循環(huán)由順序執(zhí)行的組指令組成,這些指令重復(fù)執(zhí)行直到滿足特定條件為止。 循環(huán)為簡化程序員的任務(wù)提供了很大的幫助。

Ruby中循環(huán)語句的類型 (Types of looping statements in Ruby)

Ruby supports the following types of loops:

Ruby支持以下類型的循環(huán):

  • The while loop

    while循環(huán)

  • The for loop

    for循環(huán)

  • The do...while loop

    do ... while循環(huán)

  • The until loop

    直到循環(huán)

  • 1)while循環(huán) (1) The while loop)

    In the while loop, the condition for which the loop will run is provided at the top with while keyword. It is one of the forms of the Entry control loop and the pointer gets out when the specified condition is met.

    在while循環(huán)中,循環(huán)的運(yùn)行條件在頂部提供了while關(guān)鍵字。 它是Entry控制循環(huán)的一種形式,當(dāng)滿足指定條件時(shí)指針會跳出。

    When the number of repetitions is not fixed, it is recommended to use while loop.

    當(dāng)重復(fù)次數(shù)不固定時(shí),建議使用while循環(huán)。

    Syntax:

    句法:

    while (condition )# code to be executedend

    Example:

    例:

    num=5 while num!=0puts "Number is greater than zero"num-=1 end

    Output

    輸出量

    Number is greater than zero Number is greater than zero Number is greater than zero Number is greater than zero Number is greater than zero

    In the above code, you can observe that the number of iterations was not fixed. It was dependent upon variable which is specified in the condition.

    在上面的代碼中,您可以觀察到迭代次數(shù)不是固定的。 它取決于條件中指定的變量。

    2)for循環(huán) (2) The for loop)

    for loop is different from while loop only in the context of syntax otherwise both of them are having the same functionality. It is one of the forms of the Entry control loop and the number of iterations must be specified before the execution of the loop. It repeats over a given range of numbers.

    for循環(huán)與while循環(huán)僅在語法方面不同,否則它們都具有相同的功能。 它是Entry控制循環(huán)的一種形式,必須在執(zhí)行循環(huán)之前指定迭代次數(shù)。 它在給定的數(shù)字范圍內(nèi)重復(fù)。

    Syntax:

    句法:

    for variable_name[, variable...] in expression [do]# code to be executedend

    Example:

    例:

    i = "Includehelp" for l in 1..7 doputs i end

    Output

    輸出量

    Includehelp Includehelp Includehelp Includehelp Includehelp Includehelp Includehelp

    3)do ... while循環(huán) (3) The do...while loop)

    It is the kind of Exit control loop. It is very identical to while loop but with a difference that the condition is tested after the execution of specified statements. If you want that the expressions must execute at least for once, you should go for do...while loop.

    這是一種退出控制循環(huán)。 它與while循環(huán)非常相同,但不同之處在于在執(zhí)行指定語句之后測試條件。 如果希望表達(dá)式必須至少執(zhí)行一次,則應(yīng)執(zhí)行do ... while循環(huán)。

    Syntax:

    句法:

    loop do# code blockbreak if Conditionend

    Example:

    例:

    num = 0 loop do puts "Includehelp.com"num+=1if num==8breakend end

    Output

    輸出量

    Includehelp.com Includehelp.com Includehelp.com Includehelp.com Includehelp.com Includehelp.com Includehelp.com Includehelp.com

    4)直到循環(huán) (4) The until loop)

    until loop is the antonym of while loop as per the functionality. While loop is terminated when the condition becomes false but the until loop is terminated when the Boolean expression evaluates to be true. It is one of the examples of Entry control loop where the condition is specified before the execution of expressions.

    根據(jù)功能,直到循環(huán)是while循環(huán)的反義詞。 當(dāng)條件變?yōu)榧贂r(shí),while循環(huán)終止,但是當(dāng)布爾表達(dá)式的值為真時(shí),直到循環(huán)終止。 它是Entry控制循環(huán)的示例之一,其中在執(zhí)行表達(dá)式之前指定條件。

    Syntax:

    句法:

    until conditional [do]# code to be executedend

    Example:

    例:

    num = 8 until num == 13 doputs "Hi there! Welcome to the tutorial"num+=1 end

    Output

    輸出量

    Hi there! Welcome to the tutorial Hi there! Welcome to the tutorial Hi there! Welcome to the tutorial Hi there! Welcome to the tutorial Hi there! Welcome to the tutorial

    翻譯自: https://www.includehelp.com/ruby/loops.aspx

    ruby循環(huán)

    總結(jié)

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

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