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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ruby循环_Ruby循环

發布時間:2025/3/11 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby循环_Ruby循环 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ruby循環

Ruby循環 (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.

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

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

Ruby supports the following types of loops:

Ruby支持以下類型的循環:

  • The while loop

    while循環

  • The for loop

    for循環

  • The do...while loop

    do ... while循環

  • The until loop

    直到循環

  • 1)while循環 (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循環中,循環的運行條件在頂部提供了while關鍵字。 它是Entry控制循環的一種形式,當滿足指定條件時指針會跳出。

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

    當重復次數不固定時,建議使用while循環。

    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.

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

    2)for循環 (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循環與while循環僅在語法方面不同,否則它們都具有相同的功能。 它是Entry控制循環的一種形式,必須在執行循環之前指定迭代次數。 它在給定的數字范圍內重復。

    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循環 (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.

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

    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)直到循環 (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.

    根據功能,直到循環是while循環的反義詞。 當條件變為假時,while循環終止,但是當布爾表達式的值為真時,直到循環終止。 它是Entry控制循環的示例之一,其中在執行表達式之前指定條件。

    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循環

    總結

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

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