好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 詳解Ruby中的循環語句的用法

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

熱門標簽:征服者企業地圖標注 AI電銷機器人 線路 柯城手機地圖如何做地圖標注 漯河電銷 天津外呼系統怎么收費 巫師3地圖標注魔力之所 外呼線路從哪里出來的 中牟外呼系統違法嗎 淮安自動外呼系統供應商

Ruby 中的循環用于執行相同的代碼塊若干次。本章節將詳細介紹 Ruby 支持的所有循環語句。
Ruby while 語句
語法

while conditional [do]
  code
end

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

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

這將產生以下結果:

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

Ruby while 修飾符
語法

code while condition
 
OR
 
begin
 code
end while conditional

當 conditional 為真時,執行 code。

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

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

這將產生以下結果:

Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
Ruby until 語句
until conditional [do]
  code
end

當 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 = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
Inside the loop i = 5
Ruby until 修飾符
語法
code until conditional
 
OR
 
begin
  code
end until conditional

當 conditional 為假時,執行 code。

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

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

這將產生以下結果:

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

Ruby for 語句
語法

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


針對 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 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value 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 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value 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 0
Value of local variable is 1
Value 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 2
Value of local variable is 3
Value of local variable is 4
Value 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
  end
end

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

Value of local variable is 0
Value 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 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
............................

您可能感興趣的文章:
  • 在Ruby中處理日期和時間的教程
  • 在Ruby中創建和使用哈希的教程
  • 舉例初步講解Ruby中的正則表達式

標簽:內江 河池 南昌 克拉瑪依 西雙版納 甘孜 大慶 棗莊

巨人網絡通訊聲明:本文標題《詳解Ruby中的循環語句的用法》,本文關鍵詞  詳解,Ruby,中的,循環,語句,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《詳解Ruby中的循環語句的用法》相關的同類信息!
  • 本頁收集關于詳解Ruby中的循環語句的用法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 日本免费色网站| 伊人久久免费| 公在厨房扒开腿让我爽了在线观看| 日本电影一级片| 极品尤物丰满暴露尤物| sm高强度榨精| 九九精品久久| 亚洲第一a亚洲| 欧洲性xxx| 18禁高潮出水呻吟娇喘MP3| 色爱A∨综合区| 短篇艳辣500篇h文小说| 一区二区三区精品牛牛| 日本三级欧美三级人妇视频黑白配| 女高在教室中自慰扒开喷白浆| 欧美精品一区二区AV白丝网站| 日比视频| 丰满老熟妇好大BBBBB四P| 嗯好深啊用力哦嗯啊| 嗯嗯啊舒服| 中国女人内?交XXXXX| 国产无套在线观看视频| 久久国产中文字幕| 91 高清 在线 制服 偷拍| 国產精品一区二区三区| 亚洲精品日韩片无码中文字幕| 三年片大全在线观看| 中文字幕成人在线| 一本一道久久综合狠狠躁牛牛影视 | 无码人妻av一区二区三区| 韩国电影小姨的诱惑| 成品网站w灬源码1688| miya亚洲私人影院在线| 被男揉吃奶60分钟视频免费看| 天天综合网,7799精品视频| 大尺度真做的女同片在线观看| 女人脱了内裤露出屁股打视频| 亚州怡红院| 亚洲AV无码区国产乱码99| 高义白洁小说全文阅读免费笔趣阁| 水蜜桃成视频人在线播放下载|