ruby循环_Ruby循环

ruby循环

Ruby循环 (Ruby Loops)

Loops are comprised of sequentially group instructions which are executed repeatedly until a certain condition is met. Loops provide great help in making the programmer's task easier.

循环由顺序执行的组指令组成,这些指令重复执行直到满足特定条件为止。 循环为简化程序员的任务提供了很大的帮助。

Ruby中循环语句的类型 (Types of looping statements in Ruby)

Ruby supports the following types of loops:

Ruby支持以下类型的循环:

  1. The while loop

    while循环

  2. The for loop

    for循环

  3. The do...while loop

    do ... while循环

  4. The until loop

    直到循环

1)while循环 (1) The while loop)

In the while loop, the condition for which the loop will run is provided at the top with while keyword. It is one of the forms of the Entry control loop and the pointer gets out when the specified condition is met.

在while循环中,循环的运行条件在顶部提供了while关键字。 它是Entry控制循环的一种形式,当满足指定条件时指针会跳出。

When the number of repetitions is not fixed, it is recommended to use while loop.

当重复次数不固定时,建议使用while循环。

Syntax:

句法:

    while (condition )
        # code to be executed
    end

Example:

例:

num=5
while num!=0
	puts "Number is greater than zero"
	num-=1
end

Output

输出量

Number is greater than zero
Number is greater than zero
Number is greater than zero
Number is greater than zero
Number is greater than zero

In the above code, you can observe that the number of iterations was not fixed. It was dependent upon variable which is specified in the condition.

在上面的代码中,您可以观察到迭代次数不是固定的。 它取决于条件中指定的变量。

2)for循环 (2) The for loop)

for loop is different from while loop only in the context of syntax otherwise both of them are having the same functionality. It is one of the forms of the Entry control loop and the number of iterations must be specified before the execution of the loop. It repeats over a given range of numbers.

for循环与while循环仅在语法方面不同,否则它们都具有相同的功能。 它是Entry控制循环的一种形式,必须在执行循环之前指定迭代次数。 它在给定的数字范围内重复。

Syntax:

句法:

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

Example:

例:

i = "Includehelp"
for l in 1..7 do
    puts i 
end

Output

输出量

Includehelp
Includehelp
Includehelp
Includehelp
Includehelp
Includehelp
Includehelp

3)do ... while循环 (3) The do...while loop)

It is the kind of Exit control loop. It is very identical to while loop but with a difference that the condition is tested after the execution of specified statements. If you want that the expressions must execute at least for once, you should go for do...while loop.

这是一种退出控制循环。 它与while循环非常相同,但不同之处在于在执行指定语句之后测试条件。 如果希望表达式必须至少执行一次,则应执行do ... while循环。

Syntax:

句法:

    loop do
        # code block
        break if Condition
    end

Example:

例:

num = 0
loop do 
	puts "Includehelp.com"
	num+=1

	if num==8
		break
	end
end

Output

输出量

Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com

4)直到循环 (4) The until loop)

until loop is the antonym of while loop as per the functionality. While loop is terminated when the condition becomes false but the until loop is terminated when the Boolean expression evaluates to be true. It is one of the examples of Entry control loop where the condition is specified before the execution of expressions.

根据功能,直到循环是while循环的反义词。 当条件变为假时,while循环终止,但是当布尔表达式的值为真时,直到循环终止。 它是Entry控制循环的示例之一,其中在执行表达式之前指定条件。

Syntax:

句法:

    until conditional [do]
        # code to be executed
    end

Example:

例:

num = 8
until num == 13 do
	puts "Hi there! Welcome to the tutorial"
	num+=1
end

Output

输出量

Hi there! Welcome to the tutorial
Hi there! Welcome to the tutorial
Hi there! Welcome to the tutorial
Hi there! Welcome to the tutorial
Hi there! Welcome to the tutorial


翻译自: https://www.includehelp.com/ruby/loops.aspx

ruby循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值