ruby推送示例_Ruby while循环示例

灰太狼 2023-03-05 09:40 66阅读 0赞

ruby推送示例

while循环 (The while loop)

In computer programming, while loop works for a particular Boolean condition. The repetitions keep on going until the specified Boolean condition does not come out to be false. You can also refer it as the iteration of if statement. The while loop comprises of two things:

在计算机编程中, while循环适用于特定的布尔条件。 重复操作一直进行到指定的布尔条件不为假为止。 您也可以将其称为if语句的迭代。 while循环包括两件事:

  1. The expression or Boolean condition

    表达式或布尔条件

  2. The loop body

    循环体

The while loop works in the manner that after testing the given condition if the result comes as true then the expressions written inside loop executes otherwise loop gets terminated. It is also known as pre-test loop as the condition is evaluated before the execution of the block. It is the example of Entry Control Loop because the condition is being checked before the execution of the loop body.

while循环的工作方式是,在测试给定条件后,如果结果为真,则执行写入循环中的表达式,否则将终止循环。 这也称为预测试循环,因为条件是在执行块之前评估的。 这是进入控制循环的示例,因为在执行循环主体之前先检查条件。

In Ruby programming language, while loop gets implemented with the help of the following syntax:

在Ruby编程语言中, while循环通过以下语法实现:

  1. while (Boolean condition )
  2. # code to be executed
  3. end

Example 1:

范例1:

  1. =begin
  2. Ruby program to Calculate the factorial of
  3. given number using while loop
  4. =end
  5. puts "Enter the number"
  6. num=gets.chomp.to_i
  7. i = 1
  8. fact = 1
  9. while i <= num #implementation of while loop
  10. fact *= i
  11. i += 1
  12. end
  13. puts "The factorial of #{num} is #{fact}"

Output

输出量

  1. Enter the number
  2. 5
  3. The factorial of 5 is 120

Example 2:

范例2:

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

Output

输出量

  1. First run:
  2. Enter the number
  3. 121
  4. The 121 is a palindrome
  5. Second run:
  6. Enter the number
  7. 566
  8. The 566 is not a palindrome

Example 3:

范例3:

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

Output

输出量

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

无限while循环 (Infinite while loop)

A situation may occur when you have to make an infinite loop deliberately. The loop will not get terminated itself you have to apply an additional statement known as “break” statement for stopping the loop. The same is demonstrated with the help of the following example:

当您必须故意进行无限循环时,可能会发生这种情况。 循环本身不会终止,您必须应用一个称为“ break”的附加语句来停止循环。 通过以下示例可以证明这一点:

  1. =begin
  2. Ruby program to make an infinite loop using while loop
  3. =end
  4. num=9
  5. while num!=0 #implementation of while loop
  6. puts "Includehelp.com"
  7. break
  8. end

Output

输出量

  1. Includehelp.com

You will observe that the loop will get terminated as soon as it will get the explicit break statement.

您会发现,循环将在获得显式的break语句后立即终止。

Let’s sum up:

让我们总结一下:

A while loop is an entry controlled loops that execute only if a given condition evaluates to true. Ruby uses the while keyword to define the while loop.

while循环是一个入口控制的循环,仅当给定条件的值为真时才执行。 Ruby使用while关键字定义while循环。

An infinite loop is a loop that never ends itself, i.e. needs an explicit exit statement.

无限循环是一个永不止息的循环,即需要一个明确的退出语句。

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

ruby推送示例

发表评论

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

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

相关阅读