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]
#expressions
for variable_name[, variable...] in expression [do]
# code to be executed
end
#expressions
end

Example 1:

范例1:

=begin 
Ruby program to demonstrate nested for loop
=end
puts "Enter upper limit"
ul=gets.chomp.to_i
puts "Enter Lower limit"
ll=gets.chomp.to_i
for i in ll..ul do
for i in 0..3 do
puts "Inner loop triggered"
end
puts "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
=end
for i in 1..5 do
for j in 1..i do
print "*"
end
puts ""
end

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

ruby 嵌套函數

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540905.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540905.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540905.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

oracle10數據庫鏈接失敗,Oracle10g出現Enterprise Manager 無法連接到數據庫實例解決辦法...

剛裝好 10g 時,把的監聽端口是1521.后來把端口改成了1568了,登上em發現Enterprise Manager 無法連接到數據庫實例 ,連接字符串的端口仍是1521,如何解決這個問題。登陸:出現下面錯誤:Enterprise …

springJdbc in 查詢,Spring namedParameterJdbcTemplate in查詢

springJdbc in 查詢,Spring namedParameterJdbcTemplate in查詢, SpringJdbc命名參數in查詢,namedParameterJdbcTemplate in查詢 >>>>>>>>>>>>>>>>>>>>>>>>>>…

oracle 11g r2版本號,Oracle 11g r2新增版本功能(二)

在11.2中,Oracle數據庫引入的版本的概念,這為應用程序的升級提供了極大的方便。這篇簡單描述版本的實現和查詢方式。前一篇簡單描述了版本,下面接著上面的例子看看Oracle是如何實現這個功能的:SQL> select synonym_name, table…

python 添加圖例_Python | 在圖例標簽中添加Sigma

python 添加圖例Sigma (𝜎) is very often used greek mathematical letters and has a higher repetition in probability. In this article, we are going to add 𝜎 using a command in matplotlib. Sigma(𝜎)是希臘數學字母中經常使用的字…

【51CTO學院】搜索V2.0——新的搜索,只為給你更好的

為了讓你能快速、準確的找到自己心儀的內容,51CTO學院產品及研發用盡了洪荒之力研發近2個月終于將搜索進行了全面升級。 搜索看似簡單,實則要做到精準和智能卻不是件易事,為了讓學員快速找到自己所需,節省找課時間,更高…

oracle擴容日志文件,ORACLE 加大日志文件

--新建臨時日志文件alter database add logfile group 4 (‘/u01/app/oracle/oradata/orcl/redo04.log‘) size 10m;alter database add logfile group 5 (‘/u01/app/oracle/oradata/orcl/redo05.log‘) size 10m;alter database add logfile group 6 (‘/u01/app/oracle/orad…

java多線程總結(二)

線程一般有6個狀態: 新建狀態:NEW 可運行狀態:RUNNABLE 休眠狀態:TIMED_WAITING 等待狀態:WAITING 阻塞狀態:BLOCKED 終止狀態“TERMINATED 當我們使用new創建線程之后,線程處于新建狀態,當調用…

scandir函數_PHP scandir()函數與示例

scandir函數PHP scandir()函數 (PHP scandir() function) The full form of scandir is "Scan Directory", the function scandir() is used to get the list of the files and directories available in the specified directory. scandir的完整格式為“ Scan Direc…

韓順平.2011最新版.玩轉oracle視頻教程筆記,韓順平.2011最新版.玩轉oracle視頻教程(筆記)...

韓順平.2011最新版.玩轉oracle視頻教程ORA-01045: user XIAOMING lacks CREATE SESSION privilege; logon denied 警告: 您不再連接到 ORACLE。 SQL> show user; USER 為 ""SQL> conn system/p; 已連接。SQL> grant connect to xiaoming; 授權成功。SQL>…

方冪序列 c+~+_C ++編程中的Trigraph序列

方冪序列 c~Trigraph Sequences in C are the set of three characters starting from double question marks (??). These set of the characters replaces by a single character specified in the below table, C 中的Trigraph序列是從雙問號( ?? )開始的三個…

oracle sysauth,sysauth$基表的用戶權限的一點分析

select privilege#,level from sysauth$ connect by grantee#prior privilege# and privilege#>0 start with grantee#:1 and privilege#>0如上的sql語句頻繁執行,其實對于遞歸sql對于自己初始oracle才一年的菜鳥一般是略去不看的,eygle前輩們有時…

ansys 內聚力_內聚力 軟件工程

ansys 內聚力凝聚 (Cohesion) In general terms, the word cohesion means the action or act of forming a united whole. According to the definition of Cambridge University, cohesion is defined as "the state of sticking together, or being in close agreement…

oracle認證都需要考哪幾個方面,Oracle OCP認證要通過哪些考試

Oracle OCP認證要通過哪些考試Oracle OCP DBA認證是所有Oracle認證中最普及的一種認證,這一認證過程是專為那些想要從事Oracle管理的專業數據庫管理人員設計的,適用于Oracle9I DBAs的OCP認證通過改進,刪除了備份和恢復以及網絡考試&#xff0…

左側固定 右側自適應三種方法

第一種&#xff1a;float 單一層浮動法 例如&#xff1a;左側固定成100px; 則核心代碼 左側&#xff1a;width:100px;float:left; 右側 width:auto;margin-left:100px; 實例&#xff1a; <!DOCTYPE html> <html> <head> <meta charset"utf-8&q…

ruby 集合 分組_在Ruby中打印集合的元素

ruby 集合 分組We have gone through the implementation of sets in Ruby. They are very similar to arrays. Now, let us see how we print the elements present in a set. There are no indexes present in the sets because sets are used to store the large elements. …

linux下tmp目錄屬性,Linux:文件夾屬性及umask

回顧&#xff1a;文件在小&#xff0c;也要占用一個Block如&#xff1a;echo > a1.logls a1.log(文件大小為1k)du a1.log(文件大小也應該為1k&#xff0c;如果不是1k&#xff0c;可能selinux是打開的)du -s a1.log文件夾的權限&#xff0c;系統中的文件夾默認權限基本上都為…

python淺復制與深復制_Python中的淺復制與深復制

python淺復制與深復制In python, the assignment operator does not copy the objects, instead, they create bindings between an object and the target. The object, if a collection that is mutable or consists of mutable items uses the copy so that one can change …

邏輯回歸 數據_數據科學中的邏輯回歸

邏輯回歸 數據邏輯回歸 (Logistic Regression) Logistic regression is an applied mathematics analysis methodology accustomed to predict a data price supported previous observations of a data set. Logistic regression has become a very important tool within the…

Dede?刪除文檔同時文章中的圖片的方法

首先,在"/include"目錄下建立"extend.func.php"文件. 然后,將以下內容保存在"extend.func.php"文件中,一共三個函數&#xff1a;//解析body數據&#xff0c;獲得所有圖片的絕對地址function GetPicsTruePath($body,$litpic){$delfiles array();…

《linux操作系統》第06章在線測試,Linux系統管理一測試題-附答案.doc

Linux系統管理一測試題-附答案Linux系統管理一測試題姓名&#xff1a;班級&#xff1a;考試時間180分鐘,ls,ifconfig,hostname,cd的程序文件在哪里which mkdir ls ifconfig hostname cd查看當前的PATH變量的值echo $PATH在根下新建一個目錄study&#xff0c;在study目錄下建子目…