ruby 嵌套函数_Ruby嵌套do ... while循环示例

ruby 嵌套函数

嵌套do ... while循环 (Nested do...while loop)

When two post-test loops i.e. do..while loops are working together for the fulfillment of a task, it is known as the nesting of the do...while loop. It, means that there are two do-while loops, first one is behaving as an outer loop and later one is acting as an inner loop. Execution of both the loops will take place in the way that first, the pointer will work with the outer loop then it will move to the inner loop as the condition is checked after the execution of the block. Again inside the block will be executed and then the condition will be checked and then the pointer will move to the outer loop for checking the specified Boolean condition of the outer loop. In this loop, both the blocks will be executed at least for once because do-while is an exit-control loop.

当两个测试后循环(即do..while循环)一起工作以完成任务时,这称为do ... while循环嵌套 。 这意味着有两个do-while循环,第一个循环充当外部循环,而后一个循环充当内部循环。 两个循环的执行将以以下方式进行:首先,指针将与外部循环一起使用,然后在执行完块后检查条件时,它将移至内部循环。 再次在该块内部执行,然后检查条件,然后指针将移至外部循环以检查外部循环的指定布尔条件。 在此循环中,这两个块将至少执行一次,因为do-while是退出控制循环。

In Ruby, Nesting of the do...while loop can be done with the help of the following syntax:

在Ruby中,可以在以下语法的帮助下完成do ... while循环的嵌套

    loop do
        # code block
        loop do
            # code block
            break if Condition
        end
        break if Condition
    end

Example 1:

范例1:

=begin 
Ruby program to check the number of Armstrong numbers 
present between two limits using a nested do-while loop
=end

puts "Enter upper limit:-"
ul=gets.chomp.to_i
puts "Enter lower limit:-"
ll=gets.chomp.to_i

loop do
	num=ul
	temp=ul
	arm=0
	loop do
		rem=num%10
		num=num/10
		arm=arm+rem*rem*rem
		if num==0
			break
		end
	end
	if temp==arm
		puts "#{temp} is armstrong"
	end
	ul=ul-1
	if ul==ll
		break
	end
end

Output

输出量

Enter upper limit:-
1000
Enter lower limit:-
100
407 is armstrong
371 is armstrong
370 is armstrong
153 is armstrong

Example 2:

范例2:

=begin 
Ruby program to check the number of numbers present 
between two limits using a nested do-while loop
=end

puts "Enter upper limit:-"
ul=gets.chomp.to_i
puts "Enter lower limit:-"
ll=gets.chomp.to_i

loop do
	num=ul
	temp=ul
	pal=0
	loop do
		rem=num%10
		num=num/10
		pal=pal*10+rem
		if num==0
			break
		end
	end
	if temp==pal
		puts "#{temp} is palindrome"
	end
	ul=ul-1
	if ul==ll
		break
	end
end

Output

输出量

Enter upper limit:-
200
Enter lower limit:-
100
191 is palindrome
181 is palindrome
171 is palindrome
161 is palindrome
151 is palindrome
141 is palindrome
131 is palindrome
121 is palindrome
111 is palindrome
101 is palindrome

In the above examples, you can observe that two do-while loops are working together to find the palindrome and Armstrong series between two ranges or limits. The outer do-while loop is working to decrease the upper limit by one so that the loop could work between those two specified numbers. The inner loop is processing each number to check whether it is palindrome/Armstrong or not.

在上面的示例中,您可以观察到两个do-while循环协同工作,找到了两个范围或界限之间的回文和Armstrong级数。 外部do-while循环正在将上限降低一,以便该循环可以在这两个指定数字之间起作用。 内循环正在处理每个数字,以检查它是否是回文/阿姆斯特朗。

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

ruby 嵌套函数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值