ruby 2.6.1_Ruby do ... while循环示例

ゝ一世哀愁。 2023-03-05 11:58 86阅读 0赞

ruby 2.6.1

do while循环 (The do while loop)

In programming, do…while loop works in the same way as a while loop with a basic difference that the specified Boolean condition is evaluated at the end of the block execution. This result in the execution of statements at least for once and later iterations depend upon the result of the Boolean expression. A process symbol and a Boolean condition is used to make a do…while iteration.

在编程中, do … while循环的工作方式与while循环相同,基本区别是在块执行结束时评估指定的布尔条件。 这导致语句至少执行一次,以后的迭代取决于布尔表达式的结果。 使用过程符号和布尔条件进行do … while迭代

In this loop, at first, the block defined inside the code is executed then the given condition is checked. Due to this property, it is also known as post-test loop.

在此循环中,首先,执行代码内部定义的块,然后检查给定条件。 由于此属性,它也称为测试后循环。

A do…while loop is an example of the exit-control loop because in this the code is executed before the evaluation of the specified condition. It runs till the Boolean condition stands to be true, once it evaluates to be false the loop gets terminated at that point of time.

do … while循环退出控制循环的一个示例,因为在这种情况下,代码是在评估指定条件之前执行的。 它一直运行到布尔条件为真为止,一旦评估为假,则该循环在该时间点终止。

In Ruby, the do…while loop is implemented with the help of the following syntax:

在Ruby中, do … while循环是通过以下语法实现的:

  1. loop do
  2. # code block to be executed
  3. break if Boolean_Expression #use of break statement
  4. end

Example 1:

范例1:

  1. =begin
  2. Ruby program to check whether the given
  3. number is Armstrong or not using a do-while loop
  4. =end
  5. puts "Enter the number"
  6. num=gets.chomp.to_i
  7. temp=num
  8. sum = 0
  9. loop do
  10. #implementation of the do-while loop
  11. rem=num%10
  12. num=num/10
  13. sum=sum+rem*rem*rem
  14. puts "Checking......"
  15. if num==0
  16. break
  17. end
  18. end
  19. if(temp==sum)
  20. puts "The #{temp} is Armstrong"
  21. else
  22. puts "The #{temp} is not Armstrong"
  23. end

Output

输出量

  1. First run:
  2. Enter the number
  3. 135
  4. Checking......
  5. Checking......
  6. Checking......
  7. The 135 is not Armstrong
  8. Second run:
  9. Enter the number
  10. 1
  11. Checking......
  12. The 1 is Armstrong

Example 2:

范例2:

  1. =begin
  2. Ruby program to check wether the given number
  3. is having five or not using do-while loop
  4. =end
  5. puts "Enter the number"
  6. num=gets.chomp.to_i
  7. temp=num
  8. sum = 0
  9. flag=false #flag is a Boolean variable
  10. loop do
  11. #implementation of the do-while loop
  12. rem=num%10
  13. num=num/10
  14. if(rem==5)
  15. flag=true #flag is made true if the presence is found
  16. end
  17. puts "Checking......"
  18. if num==0
  19. break
  20. end
  21. end
  22. if(flag==true)
  23. puts "We have found the presence of five in #{temp}"
  24. else
  25. puts "We have not found the presence of five in #{temp}"
  26. end

Output

输出量

  1. First run:
  2. Enter the number
  3. 1234566
  4. Checking......
  5. Checking......
  6. Checking......
  7. Checking......
  8. Checking......
  9. Checking......
  10. Checking......
  11. We have found the presence of five in 1234566
  12. Second run:
  13. Enter the number
  14. 198762
  15. Checking......
  16. Checking......
  17. Checking......
  18. Checking......
  19. Checking......
  20. Checking......
  21. We have not found the presence of five in 198762

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

ruby 2.6.1

发表评论

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

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

相关阅读

    相关 do-while循环

    do… while 语句其实是 while 语句的一个变体。该循环会先执行一次代码块,然后对条件表达式进行判断,如果条件为真,就会重复执行循环体,否则退出循环 do