Ruby 循環(huán) - while、for、until、break、redo 和 retry
Ruby循環(huán)
例如,您想打印一個(gè)字符串十次。您可以鍵入十個(gè)打印語(yǔ)句,但使用循環(huán)更容易。您唯一需要做的就是設(shè)置一個(gè)循環(huán)來(lái)執(zhí)行同一代碼塊指定的次數(shù)。這里我們討論了 Ruby 支持的循環(huán)語(yǔ)句。
Ruby while 語(yǔ)句:
while語(yǔ)句很簡(jiǎn)單,只要條件成立就會(huì)重復(fù)執(zhí)行代碼。 while 循環(huán)的條件通過(guò)保留字“do”、換行符、反斜杠 \ 或分號(hào)與代碼分隔。
句法:
while conditional [do]
code
end
例子:
以下代碼打印數(shù)字 0 到 10。在進(jìn)入循環(huán)之前檢查條件 a < 10,然后執(zhí)行主體,然后再次檢查條件。當(dāng)條件結(jié)果為 false 時(shí),循環(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 語(yǔ)句中,“do”關(guān)鍵字是可選的。下面的循環(huán)與上面的循環(huán)等效:
x = 1
y = 11
while x < y
print x ,". Ruby while loop.\n"
x +=1
end
Ruby while 修飾符:
與 if 和 except 一樣,while 也可以用作修飾符。
x = 0
x += 1 while x < 10
p x # prints 10
您可以使用 begin 和 end 創(chuàng)建一個(gè) while 循環(huán),在條件之前運(yùn)行一次循環(huán)體:
x = 0
begin
x += 1
end while x <10
p x # prints 10
Ruby 直到聲明:
當(dāng)條件為假時(shí),until 循環(huán)就會(huì)執(zhí)行。直到循環(huán)的條件通過(guò)保留字“do”、換行符、反斜杠\或分號(hào)與代碼分隔。與 while 循環(huán)一樣,do 是可選的。
句法:
until conditional [do]
code
end
例子:
以下腳本打印數(shù)字 1 到 10。與 while 循環(huán)一樣,進(jìn)入循環(huán)時(shí)以及每次執(zhí)行循環(huán)體時(shí)都會(huì)檢查條件 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)建一個(gè)until循環(huán),該循環(huán)在條件之前運(yùn)行一次主體:
x = 0
begin
x += 1
end until x <10
p x # prints 1
Ruby 聲明:
與大多數(shù)其他語(yǔ)言一樣,Python 也有 for 循環(huán),for 循環(huán)由 for 后跟一個(gè)變量組成,該變量包含迭代參數(shù),后跟 in 以及要使用每個(gè)迭代的值。
與 while 和 Until 一樣,do 是可選的。
for 循環(huán)與使用each 類似,但不會(huì)創(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 : 4
Ruby 中斷語(yǔ)句:
Break 語(yǔ)句用于提前終止塊。您還可以使用 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 2
Ruby 下一個(gè)聲明:
下一個(gè)語(yǔ)句用于跳過(guò)當(dāng)前迭代的其余部分。如果在塊內(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 : 6
Ruby 重做語(yǔ)句:
redo 語(yǔ)句用于重做當(dāng)前迭代:
句法:
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
15
Ruby觸發(fā)器
觸發(fā)器用于處理與 ruby?? -n 或 ruby?? -p 一起使用的 ruby?? 單行程序中的文本。觸發(fā)器的形式是一個(gè)表達(dá)式,指示觸發(fā)器何時(shí)開(kāi)啟,..(或…),然后是一個(gè)表達(dá)式,指示觸發(fā)器何時(shí)關(guān)閉。當(dāng)觸發(fā)器打開(kāi)時(shí),它將繼續(xù)評(píng)估為真,關(guān)閉時(shí)評(píng)估為假。觸發(fā)器必須用在條件語(yǔ)句中,例如 if、while、unless、until 等。
例子:
在以下示例中,開(kāi)啟條件為 n==12。觸發(fā)器最初在 10 和 11 時(shí)關(guān)閉(假),但在 12 時(shí)變?yōu)殚_(kāi)啟(真),并在 18 之前保持開(kāi)啟狀態(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://zijieyoumin.cn/blog/531.html
轉(zhuǎn)載時(shí)必須以鏈接形式注明原始出處及本聲明。
如果本文對(duì)你有幫助,那么請(qǐng)你贊助我,讓我更有激情的寫下去,幫助更多的人。