ruby 嵌套函数_Ruby嵌套直到循环带有示例

ruby 嵌套函数

嵌套直到循环 (Nested until loop)

Alike for, while, and do...while, until loop can also be nested for meeting the specific purpose. In this type of nesting, two until loops work in the combination such that at first, the outer loop is triggered which results in the execution of the inner loop. The outer Until loop is not invoked as long as the inner loop does not comes out to be true. It is a kind of Entry control loop which simply means that first the outer Boolean expression is processed; if it is false then the pointer will move to the inner loop to check inner condition. The whole execution will take place in the same manner.

与for, whiledo ... while一样直到循环也可以嵌套以满足特定目的。 在这种嵌套中,两个直到循环在组合中起作用,这样一来,首先触发外循环,从而执行内循环。 只要内部循环不成立,就不会调用外部的直到循环。 这是一种Entry控制循环,仅表示首先处理外部布尔表达式;然后进行处理。 如果为假,则指针将移至内部循环以检查内部条件。 整个执行将以相同的方式进行。

Syntax:

句法:

    until conditional [do]
        until conditional [do]
            # code to be executed
        end       
        # code to be executed
    end

Example 1:

范例1:

=begin 
Ruby program to find the sum of numbers lying 
between two limits using nested until loop
=end

puts "Enter the Upper limit"
ul=gets.chomp.to_i
puts "Enter the Lower limit"
ll=gets.chomp.to_i

until ul==ll
	num=ul
	temp=ul
	sum = 0

	until num==0
		#implementation of until loop
		rem=num%10
		num=num/10
		sum=sum+rem
	end

	puts "The sum of #{temp} is #{sum}"
	ul=ul-1
end

Output

输出量

Enter the Upper limit
1000
Enter the Lower limit
980
The sum of 1000 is 1
The sum of 999 is 27
The sum of 998 is 26
The sum of 997 is 25
The sum of 996 is 24
The sum of 995 is 23
The sum of 994 is 22
The sum of 993 is 21
The sum of 992 is 20
The sum of 991 is 19
The sum of 990 is 18
The sum of 989 is 26
The sum of 988 is 25
The sum of 987 is 24
The sum of 986 is 23
The sum of 985 is 22
The sum of 984 is 21
The sum of 983 is 20
The sum of 982 is 19
The sum of 981 is 18

Example 2:

范例2:

Pattern printing: Print the following pattern

图案打印:打印以下图案

    0
    01
    012
    0123
    01234

Code:

码:

=begin 
Ruby program to print the given pattern.
=end

num=0

until (num==6)
	j=0
	until(j==num)
		print j
		j+=1
	end
	puts ""
	num+=1
end


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

ruby 嵌套函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值