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

左手的ㄟ右手 2023-03-05 09:39 92阅读 0赞

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:

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

  1. until conditional [do]
  2. # code to be executed
  3. end

Example 1:

范例1:

  1. =begin
  2. Ruby program to print a message 10 times using until loop
  3. =end
  4. num=0
  5. until num==10
  6. puts "Hello there! Message from Includehelp.com! Happy learning!"
  7. num=num+1
  8. end

Output

输出量

  1. Hello there! Message from Includehelp.com! Happy learning!
  2. Hello there! Message from Includehelp.com! Happy learning!
  3. Hello there! Message from Includehelp.com! Happy learning!
  4. Hello there! Message from Includehelp.com! Happy learning!
  5. Hello there! Message from Includehelp.com! Happy learning!
  6. Hello there! Message from Includehelp.com! Happy learning!
  7. Hello there! Message from Includehelp.com! Happy learning!
  8. Hello there! Message from Includehelp.com! Happy learning!
  9. Hello there! Message from Includehelp.com! Happy learning!
  10. Hello there! Message from Includehelp.com! Happy learning!

Example 2:

范例2:

  1. =begin
  2. Ruby program to find the sum of all
  3. digits of given number using until loop
  4. =end
  5. puts "Enter the number"
  6. num=gets.chomp.to_i
  7. temp=num
  8. sum = 0
  9. until num==0
  10. #implementation of until loop
  11. rem=num%10
  12. num=num/10
  13. sum=sum+rem
  14. end
  15. puts "The sum of #{temp} is #{
  16. sum}"

Output

输出量

  1. Enter the number
  2. 456672
  3. The sum of 456672 is 30

Example 3:

范例3:

  1. =begin
  2. Ruby program to find the count of even
  3. and odd digits in the given number
  4. using until loop
  5. =end
  6. puts "Enter the number"
  7. num=gets.chomp.to_i
  8. temp=num
  9. evecount=0
  10. oddcount=0
  11. until num==0
  12. #implementation of until loop
  13. rem=num%10
  14. num=num/10
  15. if rem%2==0
  16. puts "#{rem} is even"
  17. evecount+=1
  18. else
  19. puts "#{rem} is odd"
  20. oddcount+=1
  21. end
  22. end
  23. puts "Number of even numbers are #{evecount}"
  24. puts "Number of odd numbers are #{oddcount}"

Output

输出量

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

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

ruby推送示例

发表评论

表情:
评论列表 (有 0 条评论,92人围观)

还没有评论,来说两句吧...

相关阅读