Ruby 循環(huán) - while、for、until、break、redo 和 retry
Ruby循環(huán)
例如,您想打印一個字符串十次。您可以鍵入十個打印語句,但使用循環(huán)更容易。您唯一需要做的就是設(shè)置一個循環(huán)來執(zhí)行同一代碼塊指定的次數(shù)。這里我們討論了 Ruby 支持的循環(huán)語句。
Ruby while 語句:
while語句很簡單,只要條件成立就會重復(fù)執(zhí)行代碼。 while 循環(huán)的條件通過保留字“do”、換行符、反斜杠 \ 或分號與代碼分隔。
句法:
while conditional [do]
code
end例子:
以下代碼打印數(shù)字 0 到 10。在進入循環(huán)之前檢查條件 a < 10,然后執(zhí)行主體,然后再次檢查條件。當條件結(jié)果為 false 時,循環(huán)終止。
x = 1
y = 11
while x < y do
print x ,". Ruby while loop.\n"
x +=1
end輸出:
1. Ruby while loop.
2. Ruby while loop.
3. Ruby while loop.
4. Ruby while loop.
5. Ruby while loop.
6. Ruby while loop.
7. Ruby while loop.
8. Ruby while loop.
9. Ruby while loop.
10. Ruby while loop.在 while 語句中,“do”關(guān)鍵字是可選的。下面的循環(huán)與上面的循環(huán)等效:
x = 1
y = 11
while x < y
print x ,". Ruby while loop.\n"
x +=1
endRuby while 修飾符:
與 if 和 except 一樣,while 也可以用作修飾符。
x = 0
x += 1 while x < 10
p x # prints 10您可以使用 begin 和 end 創(chuàng)建一個 while 循環(huán),在條件之前運行一次循環(huán)體:
x = 0
begin
x += 1
end while x <10
p x # prints 10Ruby 直到聲明:
當條件為假時,until 循環(huán)就會執(zhí)行。直到循環(huán)的條件通過保留字“do”、換行符、反斜杠\或分號與代碼分隔。與 while 循環(huán)一樣,do 是可選的。
句法:
until conditional [do]
code
end例子:
以下腳本打印數(shù)字 1 到 10。與 while 循環(huán)一樣,進入循環(huán)時以及每次執(zhí)行循環(huán)體時都會檢查條件 x > 11。如果條件為假,則循環(huán)將繼續(xù)執(zhí)行。
x = 1
y = 11
until x > y do
print x ,". Ruby while loop.\n"
x +=1
end輸出:
1. Ruby while loop.
2. Ruby while loop.
3. Ruby while loop.
4. Ruby while loop.
5. Ruby while loop.
6. Ruby while loop.
7. Ruby while loop.
8. Ruby while loop.
9. Ruby while loop.
10. Ruby while loop.Ruby 直到修飾符:
就像 if 和 except 一樣,until 也可以用作修飾符。
句法:
code until conditional
OR
begin
code
end until conditional例子:
x = 0
x += 1 until x > 10
p x # prints 11這將產(chǎn)生以下結(jié)果:
您可以使用begin和end創(chuàng)建一個until循環(huán),該循環(huán)在條件之前運行一次主體:
x = 0
begin
x += 1
end until x <10
p x # prints 1Ruby 聲明:
與大多數(shù)其他語言一樣,Python 也有 for 循環(huán),for 循環(huán)由 for 后跟一個變量組成,該變量包含迭代參數(shù),后跟 in 以及要使用每個迭代的值。
與 while 和 Until 一樣,do 是可選的。
for 循環(huán)與使用each 類似,但不會創(chuàng)建新的變量范圍。
for 循環(huán)的結(jié)果值是迭代的值,除非使用了break。
現(xiàn)代 Ruby 程序中很少使用 for 循環(huán)。
句法:
for variable [, variable ...] in expression [do]
code
end例子:
for x in [1, 2, 3, 4] do
puts x
end
for x in 0..4
puts "Value of x is : #{x}"
end輸出:
1
2
3
4
Value of x is : 0
Value of x is : 1
Value of x is : 2
Value of x is : 3
Value of x is : 4Ruby 中斷語句:
Break 語句用于提前終止塊。您還可以使用 Break 從 while、for 循環(huán)中終止。
句法 :
break Statement例子:
以下示例打破 while 循環(huán):
x = 0
while true do
puts x
x += 1
break if x > 5
end輸出:
0
1
2
3
4
5例子:
以下示例打破了 for 循環(huán):
例子:
以下示例打破 while 循環(huán):
for x in 0..5
if x > 2 then
break
end
puts "Value of x is #{x}"
end輸出:
Value of x is 0
Value of x is 1
Value of x is 2Ruby 下一個聲明:
下一個語句用于跳過當前迭代的其余部分。如果在塊內(nèi)調(diào)用,則終止塊的執(zhí)行。
句法:
next Statement例子:
for x in 0..6
if x < 3 then
next
end
puts "Value of x is : #{x}"
end輸出:
Value of x is : 3
Value of x is : 4
Value of x is : 5
Value of x is : 6Ruby 重做語句:
redo 語句用于重做當前迭代:
句法:
redo Statement例子:
restart = false
for x in 1..15
if x == 10
if restart == false
puts "Re-doing when x = " + x.to_s
restart = true
redo
end
end
puts x
end輸出:
1
2
3
4
5
6
7
8
9
Re-doing when x = 10
10
11
12
13
14
15Ruby觸發(fā)器
觸發(fā)器用于處理與 ruby?? -n 或 ruby?? -p 一起使用的 ruby?? 單行程序中的文本。觸發(fā)器的形式是一個表達式,指示觸發(fā)器何時開啟,..(或…),然后是一個表達式,指示觸發(fā)器何時關(guān)閉。當觸發(fā)器打開時,它將繼續(xù)評估為真,關(guān)閉時評估為假。觸發(fā)器必須用在條件語句中,例如 if、while、unless、until 等。
例子:
在以下示例中,開啟條件為 n==12。觸發(fā)器最初在 10 和 11 時關(guān)閉(假),但在 12 時變?yōu)殚_啟(真),并在 18 之前保持開啟狀態(tài)。在 18 之后,它關(guān)閉并在 19 和 20 期間保持關(guān)閉狀態(tài)。
selected = []
10.upto 20 do |value|
selected << value if value==12..value==18
end
p selected 輸出:
[12、13、14、15、16、17、18]
版權(quán)所屬:SO JSON在線解析
原文地址:http://suancuo.cn/blog/531.html
轉(zhuǎn)載時必須以鏈接形式注明原始出處及本聲明。
如果本文對你有幫助,那么請你贊助我,讓我更有激情的寫下去,幫助更多的人。
