久久天天躁狠狠躁夜夜免费观看,精品国产粉嫩内射白浆内射双马尾,久久国产欧美日韩精品,久久久久久性高,激情欧美成人久久综合

Ruby 循環(huán) - while、for、until、break、redo 和 retry

JSON 2024-05-10 16:17:52 3990

Ruby循環(huán)

例如,您想打印一個字符串十次。您可以鍵入十個打印語句,但使用循環(huán)更容易。您唯一需要做的就是設(shè)置一個循環(huán)來執(zhí)行同一代碼塊指定的次數(shù)。這里我們討論了 Ruby 支持的循環(huán)語句。

Ruby while 語句:

while語句很簡單,只要條件成立就會重復(fù)執(zhí)行代碼。 while 循環(huán)的條件通過保留字“do”、換行符、反斜杠 \ 或分號與代碼分隔。

句法:

while conditional [do]
   code
end

例子:

以下代碼打印數(shù)字 0 到 10。在進入循環(huán)之前檢查條件 a < 10,然后執(zhí)行主體,然后再次檢查條件。當條件結(jié)果為 false 時,循環(huán)終止。

x =  1
y =  11
while x <  y  do
  print  x ,". Ruby while loop.\n"
  x +=1 
  end

輸出:

1. Ruby while loop.
2. Ruby while loop.
3. Ruby while loop.
4. Ruby while loop.
5. Ruby while loop.
6. Ruby while loop.
7. Ruby while loop.
8. Ruby while loop.
9. Ruby while loop.
10. Ruby while loop.

在 while 語句中,“do”關(guān)鍵字是可選的。下面的循環(huán)與上面的循環(huán)等效:

x =  1
y =  11
while x <  y
  print  x ,". Ruby while loop.\n"
  x +=1 
  end

Ruby while 修飾符:

與 if 和 except 一樣,while 也可以用作修飾符。

x = 0

x += 1 while x < 10
p  x     # prints 10

您可以使用 begin 和 end 創(chuàng)建一個 while 循環(huán),在條件之前運行一次循環(huán)體:

x = 0
begin
  x += 1
end while x <10
p  x  # prints 10

Ruby 直到聲明:

當條件為假時,until 循環(huán)就會執(zhí)行。直到循環(huán)的條件通過保留字“do”、換行符、反斜杠\或分號與代碼分隔。與 while 循環(huán)一樣,do 是可選的。

句法:

until conditional [do]
   code
end

例子:

以下腳本打印數(shù)字 1 到 10。與 while 循環(huán)一樣,進入循環(huán)時以及每次執(zhí)行循環(huán)體時都會檢查條件 x > 11。如果條件為假,則循環(huán)將繼續(xù)執(zhí)行。

x =  1
y =  11
until x > y  do
  print  x ,". Ruby while loop.\n"
  x +=1 
  end

輸出:

1. Ruby while loop.
2. Ruby while loop.
3. Ruby while loop.
4. Ruby while loop.
5. Ruby while loop.
6. Ruby while loop.
7. Ruby while loop.
8. Ruby while loop.
9. Ruby while loop.
10. Ruby while loop.

Ruby 直到修飾符:

就像 if 和 except 一樣,until 也可以用作修飾符。

句法:

code until conditional

OR

begin
   code
end until conditional

例子:

x = 0
x += 1 until x > 10
p x   # prints 11

這將產(chǎn)生以下結(jié)果:

您可以使用begin和end創(chuàng)建一個until循環(huán),該循環(huán)在條件之前運行一次主體:

x = 0
begin
x += 1
end until x <10
p  x  # prints 1

Ruby 聲明:

與大多數(shù)其他語言一樣,Python 也有 for 循環(huán),for 循環(huán)由 for 后跟一個變量組成,該變量包含迭代參數(shù),后跟 in 以及要使用每個迭代的值。

與 while 和 Until 一樣,do 是可選的。

for 循環(huán)與使用each 類似,但不會創(chuàng)建新的變量范圍。

for 循環(huán)的結(jié)果值是迭代的值,除非使用了break。

現(xiàn)代 Ruby 程序中很少使用 for 循環(huán)。

句法:

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

例子:

for x in [1, 2, 3, 4] do
  puts x
end

for x in 0..4
  puts "Value of x is :  #{x}"
end

輸出:

1
2
3
4
Value of x is :  0
Value of x is :  1
Value of x is :  2
Value of x is :  3
Value of x is :  4

Ruby 中斷語句:

Break 語句用于提前終止塊。您還可以使用 Break 從 while、for 循環(huán)中終止。

句法 :

break Statement

例子:

以下示例打破 while 循環(huán):

x  = 0
while true do
  puts  x
  x += 1
  break if  x > 5
end

輸出:

0 
1 
2 
3 
4 
5

例子:

以下示例打破了 for 循環(huán):

例子:

以下示例打破 while 循環(huán):

for  x  in 0..5
         if  x > 2 then
            break
         end
         puts  "Value of  x is  #{x}"
end

輸出:

Value of  x is  0
Value of  x is  1
Value of  x is  2

Ruby 下一個聲明:

下一個語句用于跳過當前迭代的其余部分。如果在塊內(nèi)調(diào)用,則終止塊的執(zhí)行。

句法:

next Statement

例子:

for x  in  0..6
         if  x < 3 then
            next
         end
         puts "Value of x is : #{x}"
      end

輸出:

Value of x is : 3
Value of x is : 4
Value of x is : 5
Value of x is : 6

Ruby 重做語句:

redo 語句用于重做當前迭代:

句法:

redo Statement

例子:

restart = false
for x in 1..15
    if x == 10
        if restart == false
            puts "Re-doing when x = " + x.to_s
            restart = true
            redo
        end
    end
    puts x
end

輸出:

1
2
3
4
5
6
7
8
9
Re-doing when x = 10
10
11
12
13
14
15

Ruby觸發(fā)器

觸發(fā)器用于處理與 ruby?? -n 或 ruby?? -p 一起使用的 ruby?? 單行程序中的文本。觸發(fā)器的形式是一個表達式,指示觸發(fā)器何時開啟,..(或…),然后是一個表達式,指示觸發(fā)器何時關(guān)閉。當觸發(fā)器打開時,它將繼續(xù)評估為真,關(guān)閉時評估為假。觸發(fā)器必須用在條件語句中,例如 if、while、unless、until 等。

例子:

在以下示例中,開啟條件為 n==12。觸發(fā)器最初在 10 和 11 時關(guān)閉(假),但在 12 時變?yōu)殚_啟(真),并在 18 之前保持開啟狀態(tài)。在 18 之后,它關(guān)閉并在 19 和 20 期間保持關(guān)閉狀態(tài)。

selected = []
10.upto 20 do |value|
 selected << value if value==12..value==18
end
p selected 

輸出:

[12、13、14、15、16、17、18]

版權(quán)所屬:SO JSON在線解析

原文地址:http://suancuo.cn/blog/531.html

轉(zhuǎn)載時必須以鏈接形式注明原始出處及本聲明。

本文主題:

如果本文對你有幫助,那么請你贊助我,讓我更有激情的寫下去,幫助更多的人。

關(guān)于作者
一個低調(diào)而悶騷的男人。
相關(guān)文章
C語言while循環(huán)do while循環(huán)
C語言while循環(huán)do while循環(huán)
for循環(huán)的 i++ ++i 的區(qū)別
Maven的MirrorRepository 的詳細講解
md5base64的區(qū)別
oracle數(shù)據(jù)庫sql server的區(qū)別
ZeroClipboard.config is not a function ZeroClipboard is not defined 錯誤解決
Oracle與Mysql刪除重復(fù)的數(shù)據(jù),OracleMysql數(shù)據(jù)去重復(fù)
Elasticsearch教程(四) elasticsearch head 插件安裝使用
JSON格式講解,JSON獲取對象,JSONObjectJSONArray的操作
最新文章
計算機網(wǎng)絡(luò)的相關(guān)內(nèi)容 354
SOJSON V6 JavaScript 解密技巧與分析 5940
微信客服人工電話95068:如何快速解封微信賬號(2025最新指南) 11885
Java Http請求,HttpURLConnection HTTP請求丟失頭信息,Head信息丟失解決方案 5052
實用API合集分享:教你輕松獲取IP地址的API合集 8839
Linux I/O重定向 6705
Ruby 循環(huán) - while、for、until、break、redo 和 retry 3990
Node.js:全局對象 3604
如何使用終端檢查Linux上的內(nèi)存使用情況 3779
JavaScript對象詳細剖析 3252
最熱文章
免費天氣API,天氣JSON API,不限次數(shù)獲取十五天的天氣預(yù)報 745370
最新MyEclipse8.5注冊碼,有效期到2020年 (已經(jīng)更新) 703112
蘋果電腦Mac怎么恢復(fù)出廠系統(tǒng)?蘋果系統(tǒng)怎么重裝系統(tǒng)? 678428
Jackson 時間格式化,時間注解 @JsonFormat 用法、時差問題說明 561958
我為什么要選擇RabbitMQ ,RabbitMQ簡介,各種MQ選型對比 511823
Elasticsearch教程(四) elasticsearch head 插件安裝和使用 483716
Jackson 美化輸出JSON,優(yōu)雅的輸出JSON數(shù)據(jù),格式化輸出JSON數(shù)據(jù)... ... 299543
Java 信任所有SSL證書,HTTPS請求拋錯,忽略證書請求完美解決 246654
Elasticsearch教程(一),全程直播(小白級別) 232088
227528
支付掃碼

所有贊助/開支都講公開明細,用于網(wǎng)站維護:贊助名單查看

查看我的收藏

正在加載... ...