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 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.
在上面的代碼中,您可以觀察到迭代次數不是固定的。 它取決于條件中指定的變量。
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 executedendExample:
例:
i = "Includehelp" for l in 1..7 doputs i endOutput
輸出量
Includehelp Includehelp Includehelp Includehelp Includehelp Includehelp Includehelp3)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 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)直到循環 (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 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循環
總結
以上是生活随笔為你收集整理的ruby循环_Ruby循环的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#中的long类型示例_C#中带示例的
- 下一篇: appweb ejs_具有快速路线的EJ