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

首頁 > 編程 > Ruby > 正文

詳解Ruby中的循環語句的用法

2020-02-24 15:39:06
字體:
來源:轉載
供稿:網友

在Ruby編程中,While語句是最基本的重復執行語句,While重復執行的條件使您要執行的操作結束,其實這個語句可以多次執行,下面就跟著武林技術頻道小編的步伐來學習吧!
Ruby while 語句
語法

ruby;">while conditional [do]  codeend

當 conditional 為真時,執行 code。while 循環的 conditional 通過保留字 do、一個換行符、反斜線 / 或一個分號 ; ,來與 code 分離開。
實例

#!/usr/bin/ruby $i = 0$num = 5 while $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 codeend while conditional

當 conditional 為真時,執行 code。

如果 while 修飾符跟在一個沒有 rescue 或 ensure 子句的 begin 語句后面,code 會在 conditional 判斷之前執行一次。
實例

#!/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 = 4Ruby until 語句until conditional [do]  codeend

當 conditional 為假時,執行 code。until 語句的 conditional 通過保留字 do、一個換行符或一個分號,來與 code 分離開。
實例

#!/usr/bin/ruby $i = 0$num = 5 until $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 = 5Ruby until 修飾符語法code until conditional OR begin  codeend until conditional

當 conditional 為假時,執行 code。

如果 until 修飾符跟在一個沒有 rescue 或 ensure 子句的 begin 語句后面,code 會在 conditional 判斷之前執行一次。
實例

#!/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]  codeend

針對 expression 中的每個元素分別執行一次 code。
實例

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

在這里,我們已經定義了范圍 0..5。語句 for i in 0..5 允許 i 的值從 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 循環不會為局部變量創建一個新的作用域。for 循環的 expression 通過保留字 do、一個換行符或一個分號,來與 code 分離開。.
實例

#!/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/ruby for 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/ruby for 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 或 call。
實例

#!/usr/bin/ruby for 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 出現在 begin 表達式的 rescue 子句中,則從 begin 主體的開頭重新開始。

begin  do_something # 拋出的異常rescue  # 處理錯誤  retry # 重新從 begin 開始end

如果 retry 出現在迭代內、塊內或者 for 表達式的主體內,則重新開始迭代調用。迭代的參數會重新評估。

for i in 1..5  retry if some_condition # 重新從 i == 1 開始end

實例

#!/usr/bin/ruby for 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............................

以上就是我們為各位朋友們介紹的詳解Ruby中的循環語句的用法。看完上面的內容之后,是不是覺得非常的神奇呀,希望大家能繼續支持武林技術頻道!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 最新黄色电影网站 | 欧美成人免费在线视频 | 欧美精品一区二区视频 | 九九热精品视频在线 | 欧美顶级毛片在线播放小说 | 久久网综合 | 欧洲成人一区二区 | 中文区永久区 | 欧美人的天堂一区二区三区 | 亚洲最大的成人网 | 久久国产精品久久久久久电车 | 国产成人精品免高潮在线观看 | 国产九色在线播放九色 | 孕妇体内谢精满日本电影 | 国产精品久久久久久久久久久久久久久久 | 99视频观看 | 成人一级在线 | 一区二区免费 | 毛片一区二区三区四区 | freexxx69性欧美hd | 亚洲一区在线视频观看 | 免费a级毛片永久免费 | 国产99久久精品一区二区 | 亚洲午夜免费电影 | 依依成人综合 | 日本网站在线播放 | 在线播放亚洲视频 | 国产免费激情视频 | 国产一级毛片国产 | 爱爱视频天天干 | 99亚洲| 鲁丝片一区二区三区免费入口 | 日韩毛片一区二区三区 | 九九热九九爱 | 久久亚洲精品久久国产一区二区 | 蜜桃视频网站在线观看 | 最新在线黄色网址 | 色吧综合网 | 91精品国产乱码久久久久久久久 | 国产精品一区网站 | 欧美人与zoxxxx另类9 |