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

首頁 > 學院 > 開發設計 > 正文

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

2019-10-26 19:26:35
字體:
來源:轉載
供稿:網友

 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            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁夏| 定远县| 宁夏| 铁力市| 翼城县| 曲松县| 舞阳县| 西青区| 襄城县| 同心县| 浦北县| 新邵县| 简阳市| 那曲县| 嵩明县| 芦山县| 衢州市| SHOW| 仁寿县| 闸北区| 大方县| 泰和县| 玉屏| 漳平市| 安仁县| 深水埗区| 张家界市| 昌都县| 鹤峰县| 周至县| 彭水| 平山县| 晋宁县| 南陵县| 嘉善县| 微博| 包头市| 罗定市| 金坛市| 汉川市| 灵山县|