ruby循环
Loops are used to execute set of statements repeatedly based on a condition. It is sometimes necessary to execute set of statements again and again. For example, checking whether number in an array are prime or not.
循环用于根据条件重复执行语句集。 有时有必要一次又一次地执行语句集。 例如,检查数组中的数字是否为质数。
Ruby:While循环 (Ruby: While loop)
While loop is used to execute a block or segment of code repeatedly until the condition becomes false.
While循环用于重复执行代码块或代码段,直到条件变为假。
Syntax of While loop:
While循环的语法:
while (condition)
statements
end
First the condition is checked, if it is true the statements inside while block
is repeatedly. At the end of each iteration the condition is checked again. When the condition is found to be false, while
loop terminates.
首先检查条件,如果为真,则重复执行while block
内的语句。 在每次迭代结束时,将再次检查条件。 当发现条件为false时 , while
循环终止。
Consider the below example that prints "Hello, World" 5 times.
考虑下面的示例,该示例打印“ Hello,World” 5次。
![While loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/78915df8a8fbb5aac31acfef81204f2a.png)
Let's check its output now
让我们现在检查其输出
![While loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/5725807feb92d8c621afd19aa752778d.png)
Oh, Wait! What's this? It should have printed "Hello, World" only 5 times. But what happened?
等一下! 这是什么? 它应该只打印5次“ Hello,World” 。 但是发生了什么?
The variable count is initialized with the value 1 and the loop will terminate when count becomes 5. But inside the while loop, there is no statement that increments the value of count. Since the condition never becomes false it won't terminate. This is called Infinite loop
.
变量count用值1初始化,并且当count变为5时,循环将终止。 但是在while循环中,没有任何语句可以增加count的值。 由于条件永远不会为假,因此不会终止。 这称为Infinite loop
。
![While loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/461d38a150dd41eab6180b7050d0a0d7.png)
Now, in this code you could see that the value of count is incremented by 1. So, every time the loop is executed the value of count is incremented and if the value of count becomes 6, the condition fails and comes out of the loop. Now, let's see the output.
现在,在这段代码中,您可以看到count的值增加了1 。 因此,每次执行循环时, count的值都会增加,如果count的值变为6 ,则条件将失败并退出循环。 现在,让我们看一下输出。
![While loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/21c688681254164a0902d5727ccb48b9.png)
Well, that's our expected output. After the condition fails, it comes out of while block
and executes the next statement followed by while block
.
好吧,这就是我们的预期输出。 条件失败后,它从while block
出来并执行下一条语句,然后执行while block
。
The next program prints the sum of first ten numbers.
下一个程序将打印前十个数字的总和。
![While loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/3d5d7a0447aae36e1f55eeae9988bd0c.png)
Ruby:直到循环 (Ruby: Until loop)
Until loop
is very similar to while loop
.
Until loop
与while loop
非常相似。
The only difference between while loop and until loop is that while loop executes set of statements repeatedly until the condition becomes false and until loop executes set of statements repeatedly until the condition becomes true. Until
is vice versa of while
.
while循环和直到循环的唯一区别在于, while循环重复执行语句集直到条件变为false 为止 , 直到循环重复执行语句集直到条件变为true为止。 Until
是反过来的while
。
Syntax of While loop:
While循环的语法:
until (condition)
statements
end
The condition is checked, if it false it executes the statements inside until block
, when the condition becomes true, it comes out of the loop.
条件被检查,如果为假 ,则在until block
内执行语句, until block
条件变为true时 ,才退出循环。
![Until loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/63c4061352e970c1b24e0e49f9d8912d.png)
The loop executes until count becomes 6. When count becomes 6, the condition will be true and exits the loop. The output of the program is :
循环执行直到count变为6为止。 当count变为6时,条件将为true并退出循环。 该程序的输出为:
![Until loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/eec44de2a94866cde83c872129490593.png)
Program to print the sum of first 10 numbers
程序打印前10个数字的总和
![Until loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/1c11194cf386f36918b4a58dd864a755.png)
The output of the above program is :
上面程序的输出是:
![Until loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/5aaa196509b52ce1010313a9fc62500c.png)
Ruby:每个迭代器 (Ruby: Each Iterator)
![Each loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/344eebe37ed20bb167d89c9695e08627.png)
Each Iterator is used to iterate over an array
It pulls out each element of a container or an array and assigns to the variable x. Using the value of the variable x, we can do our desired operation.
它提取容器或数组的每个元素,并分配给变量x 。 使用变量x的值,我们可以执行所需的操作。
Ruby:用于循环 (Ruby: For in loop)
It's just like while
and unless
loop to execute set of statements repeatedly. Using this we can also iterate through containers.
就像while
和unless
循环来重复执行一组语句。 使用此方法,我们还可以遍历容器。
![For in loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/4d2455c37283b8f628440ad97f5ee9bc.png)
In this code, it pulls out each element of the array nums and storing it in a single element(variable) num. Then using that value, we can do the required operation. The output of the above code is :
在此代码中,它提取数组nums的每个元素,并将其存储在单个元素(变量) num中 。 然后使用该值,我们可以执行所需的操作。 上面代码的输出是:
![For in loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/47ef5d0ebde453d11394032f6302aacb.png)
To sum 10 numbers using for in loop:
使用for循环求和10个数字:
![For in loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/be80c6edc59e7b0076f7b88100d1a511.png)
And its output is :
其输出为:
![For in loop example in Ruby](https://i-blog.csdnimg.cn/blog_migrate/43852dad8813e5f86f5713ee32af5ebe.png)
ruby循环