ruby推送示例_Ruby直到示例循环

ruby推送示例

直到循环 (The until loop)

The until loop is one of the great features of Ruby which makes it different from other programming languages. The support of until loop specifies how much user-friendly language Ruby is?

直到循环是Ruby的重要功能之一,这使其与其他编程语言有所不同。 对直到循环的支持指定了Ruby用户友好语言是多少?

The until loop is just the opposite of while loop if we justify it in terms of functionality. while loop is executed as long as the Boolean condition stands to be false but in case of until loop, it executes as long as the Boolean condition does not come out to be true. It is the example of the entry-control loop where the specified condition is checked prior to the execution of the loop's body.

如果我们从功能上证明它,则直到循环while循环正好相反。 while循环是只要布尔条件成立的情况是假的,但直到循环的情况下执行,因为布尔条件不出来是真实的它执行长。 这是进入控制循环的示例,其中在执行循环主体之前检查指定的条件。

The until loop is implemented with the help of the following syntax:

使用以下语法可以实现直到循环

    until conditional [do]
        # code to be executed
    end

Example 1:

范例1:

=begin 
Ruby program to print a message 10 times using until loop
=end

num=0

until num==10
    puts "Hello there! Message from Includehelp.com! Happy learning!"
    num=num+1
end

Output

输出量

Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!

Example 2:

范例2:

=begin 
Ruby program to find the sum of all 
digits of given number using until loop
=end

puts "Enter the number"
num=gets.chomp.to_i

temp=num
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}"

Output

输出量

Enter the number
456672
The sum of 456672 is 30

Example 3:

范例3:

=begin 
Ruby program to find the count of even 
and odd digits in the given number 
using until loop
=end

puts "Enter the number"
num=gets.chomp.to_i

temp=num
evecount=0
oddcount=0

until num==0
	#implementation of until loop
	rem=num%10
	num=num/10
	if rem%2==0
		puts "#{rem} is even"
		evecount+=1
	else
		puts "#{rem} is odd"
		oddcount+=1
	end
end

puts "Number of even numbers are #{evecount}"
puts "Number of odd numbers are #{oddcount}"

Output

输出量

Enter the number
7896532
2 is even
3 is odd
5 is odd
6 is even
9 is odd
8 is even
7 is odd
Number of even numbers are 3
Number of odd numbers are 4


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

ruby推送示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值