麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > 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............................

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天天撸日日夜夜 | 黄污网址 | 久久久精品视频在线观看 | 国产91丝袜在线播放0 | 日本在线高清 | 素人视频免费观看 | 毛片在线免费观看网址 | 一级黄色影片在线观看 | 国产成人在线视频播放 | 国产毛片在线 | 国产一级二级在线播放 | 伊人在线 | 911精品影院在线观看 | 亚洲卡通动漫在线观看 | 免费试看av | 国产精品999在线观看 | 香蕉成人在线观看 | 欧美激情综合网 | 香蕉久久久久久 | 久久精品视频在线看99 | 91精品国产日韩91久久久久久360 | 久久久久免费精品国产小说色大师 | 国产免费久久久久 | 娇妻被各种姿势c到高潮小说 | 狼伊千合综网中文 | 成人综合一区二区 | 成人爽a毛片免费啪啪红桃视频 | 国产色妞影院wwwxxx | 2021国产精品| 国产精品一区二区三区在线播放 | 午夜视频在线观看免费视频 | 日本在线播放一区二区 | 天天碰天天操 | 午夜伦情电午夜伦情电影 | 国产精品视频在线观看免费 | 羞羞视频免费网站男男 | 国产在线区| 九一免费版在线观看 | 欧美自拍 | 国产成人综合在线观看 | 国产免费激情视频 |