a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产成人免费视频 | 欧美一区二区人人喊爽 | 成人精品一区 | 中文字幕一区二区三区精彩视频 | 日韩在线视频第一页 | 黄色大片免费网站 | 日韩啊啊啊 | 中文av在线免费观看 | 久久国产欧美日韩精品 | 中文字幕亚洲一区二区三区 | 日本免费福利视频 | 中文字幕在线第二页 | 少妇被粗大的猛烈进大胸视频 | 毛片网子 | 国产美女自拍视频 | 欧美成人精品一区二区男人看 | 高潮毛片又色又爽免费 | 日韩精品一区二区三区在线 | 国产91成人在在线播放 | 欧美a免费 | 欧美一区永久视频免费观看 | 久久亚洲免费 | 在线精品观看 | 一级激情片 | 欧美日韩三区 | 亚洲国产aⅴ成人精品无吗 亚洲h | 午夜草逼| 91在线精品一区二区 | www.日本三级 | 国产精品成av人在线视午夜片 | 欧美午夜精品一区二区三区电影 | 亚洲自拍av在线 | av在线播放一区二区 | 人人爽人人爱 | 综合久久久久 | 免费黄色av | 欧美日韩一区二区视频在线观看 | 国产精品一区二区久久精品涩爱 | www.色网| 亚洲高清在线观看 | 国产日本在线视频 |