<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Ruby 循環 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 或者 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 id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看