ruby循环_Ruby中的循环

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

Let's check its output now

让我们现在检查其输出

While loop example in Ruby

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

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

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

Ruby:直到循环 (Ruby: Until loop)

Until loop is very similar to while loop.

Until loopwhile 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

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

Program to print the sum of first 10 numbers

程序打印前10个数字的总和

Until loop example in Ruby

The output of the above program is :

上面程序的输出是:

Until loop example in Ruby

Ruby:每个迭代器 (Ruby: Each Iterator)

Each loop example in Ruby
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.

就像whileunless循环来重复执行一组语句。 使用此方法,我们还可以遍历容器。

For in loop example in Ruby

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

To sum 10 numbers using for in loop:

使用for循环求和10个数字:

For in loop example in Ruby

And its output is :

其输出为:

For in loop example in Ruby

翻译自: https://www.studytonight.com/ruby/loops-in-ruby

ruby循环

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值