日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ruby 嵌套函数_Ruby嵌套有示例的循环

發布時間:2025/3/11 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby 嵌套函数_Ruby嵌套有示例的循环 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ruby 嵌套函數

嵌套循環 (Nested for loop)

Nesting of for loop means one for loop is available inside another for loop. It means that there are two loops, then the first one is an outer loop and the second one is the inner loop. Execution will take place in the manner that first the outer for loop is triggered, then if the condition is matched the pointer will be passed to the inner loop. Here also if the condition is satisfied, the inner for loop body will be executed until specified condition does not comes out to be false. Once the inner loop completes its execution, the pointer will be passed back to the outer for loop for its successful execution.

嵌套for循環意味著一個for循環可在另一個for循環內使用 。 這意味著有兩個循環,然后第一個是外部循環,第二個是內部循環。 執行將以首先觸發外部for循環的方式進行,然后如果條件匹配,則指針將被傳遞到內部循環。 同樣,如果滿足條件,則將執行內部for循環主體,直到指定條件不為假為止。 內部循環完成執行后,指針將被傳遞回外部for循環以成功執行。

In Ruby, Nesting of for loop can be done with the help of the following syntax:

在Ruby中,可以使用以下語法來完成for循環的嵌套 :

for variable_name[, variable...] in expression [do]#expressionsfor variable_name[, variable...] in expression [do]# code to be executedend#expressionsend

Example 1:

范例1:

=begin Ruby program to demonstrate nested for loop =endputs "Enter upper limit" ul=gets.chomp.to_i puts "Enter Lower limit" ll=gets.chomp.to_ifor i in ll..ul dofor i in 0..3 doputs "Inner loop triggered"endputs "Outer loop triggered" end

Output

輸出量

Enter upper limit 90 Enter Lower limit 85 Inner loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Outer loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Outer loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Outer loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Outer loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Outer loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Inner loop triggered Outer loop triggered

You can observe in the above example that first the outer for loop is invoked then the pointer has moved to inner loop and it is printing the message for times.

您可以在上面的示例中觀察到,首先調用了外部for循環,然后指針移至了內部循環,并且多次打印該消息。

Once the inner for loop completes its task, the pointer goes back to the outer loop.

內部for循環完成其任務后,指針將返回到外部循環。

Example 2:

范例2:

Patterns can also be printed using nested for loop. Now let us have a quick view of how the following pattern can be printed.

也可以使用嵌套的for循環來打印圖案。 現在讓我們快速了解如何打印以下圖案。

* ** *** **** *****

Code:

碼:

=begin Ruby program to print a pattern using nested for loop =endfor i in 1..5 dofor j in 1..i doprint "*"endputs "" end

翻譯自: https://www.includehelp.com/ruby/nested-for-loop-with-examples.aspx

ruby 嵌套函數

總結

以上是生活随笔為你收集整理的ruby 嵌套函数_Ruby嵌套有示例的循环的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。