国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Ruby > 正文

Ruby中的循環語句的用法教程

2020-10-29 19:39:59
字體:
來源:轉載
供稿:網友

 Ruby中的循環用于執行相同的代碼塊指定的次數。本章將詳細介紹Ruby支持的循環語句。
Ruby while 語句:
語法:

while conditional [do]
   code
end

執行代碼當條件為true時。while循環的條件是代碼中的保留字,換行,反斜杠(/)或一個分號隔開。
實例:

#!/usr/bin/ruby$i = 0$num = 5while $i < $num do  puts("Inside the loop i = #$i" )  $i +=1end

這將產生以下結果:

Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4

Ruby while 修辭符:
語法:

code while condition

OR

begin
  code
end while conditional

執行代碼,當條件為true。

如果while 修飾符緊跟一個begin 語句但是沒有 rescue 或 ensure 子句, 代碼被執行前一次條件求值。
實例:

#!/usr/bin/ruby$i = 0$num = 5begin  puts("Inside the loop i = #$i" )  $i +=1end while $i < $num

這將產生以下結果:

Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4

Ruby until 語句:

until conditional [do]
   code
end

執行代碼當條件為false。until 條件語句從代碼分離的保留字,換行符或分號。
語句:

#!/usr/bin/ruby$i = 0$num = 5until $i > $num do  puts("Inside the loop i = #$i" )  $i +=1;end

這將產生以下結果:

Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4Inside the loop i = 5

Ruby until 修辭符:
語法:

code until conditional

OR

begin
   code
end until conditional

執行代碼當條件為 false。

如果 until 修辭符跟著 begin 語句但沒有 rescue 或 ensure 子句, 代碼一旦被執行在條件求值之前。
例子:

#!/usr/bin/ruby$i = 0$num = 5begin  puts("Inside the loop i = #$i" )  $i +=1;end until $i > $num

這將產生以下結果:

Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4Inside the loop i = 5

Ruby for 語句:
語法:

for variable [, variable ...] in expression [do]
   code
end

一次執行代碼的每個元素在 in 表達式。
實例:

#!/usr/bin/rubyfor i in 0..5  puts "Value of local variable is #{i}"end

這里我們定義的范圍 0 .. 5 。因為在語句 for i in 0..5 將允許取值的范圍從0到5(含5),這將產生以下結果:

Value of local variable is 0Value of local variable is 1Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5

 for...in 循環幾乎是完全等同于:

(expression).each do |variable[, variable...]| code end

除了一個for循環不創建一個新的局部變量的范圍。一個循環的表情從代碼分離,保留字,一個換行符,或分號。
例子:

#!/usr/bin/ruby(0..5).each do |i|  puts "Value of local variable is #{i}"end

這將產生以下結果:

Value of local variable is 0Value of local variable is 1Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5

Ruby break 語句:
語法:

break

終止大多數內部的循環。終止塊內的方法返回nil如果調用的方法與相關塊。
實例:

#!/usr/bin/rubyfor i in 0..5  if i > 2 then   break  end  puts "Value of local variable is #{i}"end

這將產生以下結果:

Value of local variable is 0Value of local variable is 1Value of local variable is 2

Ruby next 語句:
語法:

next

跳轉到最內部循環的下一次迭代。如果調用塊一個塊內終止執行(帶 yield 或調用返回 nil )。
例子:

#!/usr/bin/rubyfor i in 0..5  if i < 2 then   next  end  puts "Value of local variable is #{i}"end

這將產生以下結果:

Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5

Ruby redo 語句:
語法:

redo

會重新啟動啟動這個最內部的循環迭代,而不檢查循環條件。

會重新啟動 yield or call ,如果一個塊內調用。
例子:

#!/usr/bin/rubyfor i in 0..5  if i < 2 then   puts "Value of local variable is #{i}"   redo  endend

這將產生以下結果,將執行無限循環:

Value of local variable is 0Value of local variable is 0............................

Ruby retry 語句:
語法:

retry

如果 retry 表達出現在 rescue 子句,則從開始重新開始。

begin  do_something # exception raisedrescue  # handles error  retry # restart from beginningend

如果出現重試迭代,塊,或體內的表達,重新啟動迭代調用。迭代器的參數條件將重新計算。

for i in 1..5  retry if some_condition # restart from i == 1end

實例:

#!/usr/bin/rubyfor i in 1..5  retry if i > 2  puts "Value of local variable is #{i}"end

這將產生以下結果,將進入無限循環:

Value of local variable is 1Value of local variable is 2Value of local variable is 1Value of local variable is 2Value of local variable is 1Value of local variable is 2............................

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岚皋县| 南木林县| 嘉鱼县| 湘潭县| 澄城县| 田东县| 闸北区| 门源| 吉木萨尔县| 佛冈县| 天全县| 玛曲县| 留坝县| 江达县| 乃东县| 兰西县| 宝清县| 宣武区| 丽江市| 兖州市| 中方县| 永清县| 连平县| 包头市| 龙山县| 兴宁市| 香港 | 芦山县| 内黄县| 临夏市| 增城市| 临桂县| 博爱县| 白山市| 丹阳市| 永善县| 高雄县| 宜阳县| 麟游县| 松江区| 阿拉善右旗|