ruby推送示例_Ruby for循環示例

ruby推送示例

for循環 (The for loop)

In programming, for loop is a kind of iteration statement which allows the block to be iterated repeatedly as long as the specified condition is not met or a specific number of times that the programmer knows beforehand. A for loop is made of two parts:

在編程中, for循環是一種迭代語句,只要不滿足指定條件或程序員事先知道的特定次數,就可以重復迭代該塊。 for循環由兩部分組成:

  1. The header part

    標頭部分

  2. The actual body

    實際的身體

The header part is used to specify the number of iterations. Most of the times it explicitly mentions the count of iterations with the help of a variable. for loop is generally used when the number of iterations is explicitly known before the execution of the statement declared within its block. The actual body contains the expressions or statements which will be implemented once per repetition.

標頭部分用于指定迭代次數。 在大多數情況下,它借助變量明確提及迭代次數。 當在其塊內聲明的語句執行之前明確知道迭代次數時,通常使用for循環 。 實際主體包含將重復執行一次的表達式或語句。

It is a kind of Entry control loop. Generally, you can easily make an infinite loop through for loop by using the following syntax:

這是一種Entry控制循環。 通常,您可以使用以下語法輕松地通過for循環進行無限循環:

    for(;;)
{
#body
}

In Ruby, for loop is implemented with the help of the following syntax:

在Ruby中, for循環是通過以下語法實現的:

    for variable_name[, variable...] in expression [do]
# code to be executed
end 

Example 1:

范例1:

=begin
Ruby program to print the table of the number 
specified by the user using for loop	
=end
puts "Enter a number"
num=gets.chomp.to_i
for i in 1..10 		#implementation of for loop for 
#pre-specified range 1..10
k=i*num
puts "#{num} * #{i}  = #{k}"
i+=1		#incrementing the counter variable
end

Output

輸出量

Enter a number
89
89 * 1  = 89
89 * 2  = 178
89 * 3  = 267
89 * 4  = 356
89 * 5  = 445
89 * 6  = 534
89 * 7  = 623
89 * 8  = 712
89 * 9  = 801
89 * 10  = 890

Example 2:

范例2:

=begin
Ruby program to print the list of the odd and even 
numbers where the lower limit is specified by the user 
and the upper limit is 100 using for loop		
=end
puts "Enter the lower limit(ul is 100)"
num=gets.chomp.to_i
if (num>=100)    #lower limit can not be 
#equal to or greater than upper limit 
puts "Invalid lower limit"
else
for i in num..100 #implementation of for loop for 
#pre-specified range num..100
if (i%2==0)
puts "#{i} is even"
else
puts "#{i} is odd"
end
i=i+1 	#incrementing the counter variable
end
end

Output

輸出量

First run:
Enter the lower limit
76
76 is even
77 is odd
78 is even
79 is odd
80 is even
81 is odd
82 is even
83 is odd
84 is even
85 is odd
86 is even
87 is odd
88 is even
89 is odd
90 is even
91 is odd
92 is even
93 is odd
94 is even
95 is odd
96 is even
97 is odd
98 is even
99 is odd
100 is even
Second run:
Enter the lower limit
900
Invalid lower limit

The for loop is one of the most commonly used loops in programming. As it is preferable and has easy syntax.

for循環是編程中最常用的循環之一。 最好是它并且語法簡單。

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

ruby推送示例

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

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

相關文章

《DBNotes: Buffer Pool對于緩沖頁的鏈表式管理》

目錄Buffer Pool回顧Buffer Pool內部組成freelistflushlistLRU鏈表管理以及改進Buffer Pool回顧 我們知道針對數據庫的增刪改刪操作都是在Buffer Pool中完成的,一條sql的執行步驟可以認為是這樣的: 1、innodb存儲引擎首先在緩沖池中查詢有沒有對應的數據…

一個延時調用問題

如果用下面第1行的寫法,調用 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector(removeFromSuperview) object:nil]; 可以生效 如果用下面第3行的寫法,調用 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:…

onclicklistener 方法使用匯總

相信很多像我一樣的新手學習ANDROID開發會遇到這個問題,通過這幾天的歸類和總結,將我的理解寫在下面,歡迎大家一起前來討論: 以按鈕BUTTON的監聽事件為例,以下的監聽實現都是等價的: 1.使用接口繼承按鈕監聽…

《源碼分析轉載收藏向—數據庫內核月報》

月報原地址: 數據庫內核月報 現在記錄一下,我可能需要參考的幾篇文章吧,不然以后還得找: MySQL 代碼閱讀 MYSQL開源軟件源碼閱讀小技巧 MySQL 源碼分析 聚合函數(Aggregate Function)的實現過程 MySQL …

vim中的jk為什么是上下_JK的完整形式是什么?

vim中的jk為什么是上下JK:開玩笑 (JK: Just Kidding) JK is an abbreviation of "Just Kidding". JK是“ Just Kidding”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Faceb…

百度歸來的學長做報告

今天下午下課到現在才總算閑下來,本來計劃這個時間應該是讀英語,做英語模擬題的時間,但是,我不得不寫點什么來記錄下剛才的事——在百度實習并且簽下工作的學長做報告。 原本認為每個人的成功(請允許我目前的眼光簽個好…

轉:Google論文之三----MapReduce

文章來自于:http://www.cnblogs.com/geekma/p/3139823.html MapReduce:大型集群上的簡單數據處理 摘要 MapReduce是一個設計模型,也是一個處理和產生海量數據的一個相關實現。用戶指定一個用于處理一個鍵值(key-value)…

合約 cd 模式_CD的完整形式是什么?

合約 cd 模式CD:光盤 (CD: Compact Disc) CD is an abbreviation of "Compact Disc". CD是“ Compact Disc”的縮寫 。 It is a digital optical disc originally developed to store the audio of recordings in the format of a data file used as a p…

《DBNotes:Join算法的前世今生》

目錄NestLoopJoin算法Simple Nested-Loop JoinIndex Nested-Loop JoinBlock Nested-Loop JoinBatched Key AccessHash Join算法In-Memory Join(CHJ)On-Disk Hash Join參考鏈接在8.0.18之前,MySQL只支持NestLoopJoin算法,最簡單的就是Simple NestLoop Joi…

如何解決迅雷插件導致IE10崩潰的問題

Windows 8里面帶的IE10酷不酷?沉浸式界面果然不同凡響,IE10讓人幾乎認不出來了!這是微軟的瀏覽器么?上面這張圖是Windows8下Metro UI的新界面IE10,不過當我們切換回傳統桌面的時候,也有IE10的經典版的。好吧…

UNITY3D與iOS交互解決方案

原地址:http://bbs.18183.com/thread-456979-1-1.html 本帖最后由 啊,將進酒 于 2014-2-27 11:17 編輯 “授人以魚,不如授人以漁”,以UNITY3D調用iOS版的91SDK為例,利用C# / C / OBJ-C交互原理,本文將詳細介紹UNITY3D與iOS之間交互…

c:if equal_C ++中的std :: equal()

c:if equalequal()作為STL函數 (equal() as a STL function) Syntax: 句法: bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);Where, 哪里, InputIterator1 first iterator to start of the first sequence range I…

《DBNotes:Buffer Pool刷臟頁細節以及改進》

本筆記知識沿用之前DBNotes: Buffer Pool對于緩沖頁的鏈表式管理的部分知識 目錄獲取一個空閑頁的源碼邏輯Page_Cleaner_ThreadLRU_Manager_ThreadHazard Pointer作為驅逐算法改進參考獲取一個空閑頁的源碼邏輯 任何一個讀寫請求都需要從Buffer pool來獲取所需頁面。如果需要的…

WordPress刪除數據中標題重復文章的方法

一種是刪除重復的方法是:使用插件,大家可以去官網上下載 二種刪除重復的方法是:登錄數據庫,使用sql語句刪除,具體的語句為如下代碼: CREATE TABLE my_tmp AS SELECT MIN(ID) AS col1 FROM wp_posts GROUP BY post_titl…

hibernate配置

hibernate.cfg.xml <?xml version"1.0" encoding"UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quo…

html中表單元素_HTML中的表單元素

html中表單元素1)<input>元素 (1) The <input> Element) The <input> element is used to get input from the user in an HTML form. <input>元素用于以HTML形式從用戶獲取輸入。 <input> tag is used to get input using input element, the …

《搜索算法——DFS、BFS、回溯》

目錄深搜200. 島嶼數量695. 島嶼的最大面積130. 被圍繞的區域547. 省份數量417. 太平洋大西洋水流問題回溯廣搜111. 二叉樹的最小深度752. 打開轉盤鎖深搜與廣搜結合934. 最短的橋深搜 深搜DFS&#xff0c;在搜索到一個新節點時&#xff0c;立即對該新節點進行遍歷&#xff0c…

AP in R

AP聚類算法是目前十分火的一種聚類算法&#xff0c;它解決了傳統的聚類算法的很多問題。不僅簡單&#xff0c;而且聚類效果還不錯。這里&#xff0c;把前兩天學習的AP算法在R語言上面的模擬&#xff0c;將個人筆記拿出來與大家分享一下&#xff0c;不談AP算法的原理&#xff0c…

nginx 模塊解析

nginx的模塊非常之多&#xff0c;可以認為所有代碼都是以模塊的形式組織&#xff0c;這包括核心模塊和功能模塊&#xff0c;針對不同的應用場合&#xff0c;并非所有的功能模塊都要被用到&#xff0c;附錄A給出的是默認configure&#xff08;即簡單的http服務器應用&#xff09…

python關鍵字和保留字_Python關鍵字

python關鍵字和保留字關鍵詞 (Keywords) Keywords are the reserved words in Python programming language (and, any other programming languages like C, C, Java, etc) whose meanings are defined and we cannot change their meanings. In python programming languages…