ruby循环_Ruby循环
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 executedendExample:
例:
num=5 while num!=0puts "Number is greater than zero"num-=1 endOutput
輸出量
Number is greater than zero Number is greater than zero Number is greater than zero Number is greater than zero Number is greater than zeroIn 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 executedendExample:
例:
i = "Includehelp" for l in 1..7 doputs i endOutput
輸出量
Includehelp Includehelp Includehelp Includehelp Includehelp Includehelp Includehelp3)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 ConditionendExample:
例:
num = 0 loop do puts "Includehelp.com"num+=1if num==8breakend endOutput
輸出量
Includehelp.com Includehelp.com Includehelp.com Includehelp.com Includehelp.com Includehelp.com Includehelp.com Includehelp.com4)直到循環(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 executedendExample:
例:
num = 8 until num == 13 doputs "Hi there! Welcome to the tutorial"num+=1 endOutput
輸出量
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#中的long类型示例_C#中带示例的
- 下一篇: oracle中dbms_DBMS中的实例