ruby推送示例_Ruby while循环示例

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循环通过以下语法实现:

    while (Boolean condition )
        # code to be executed
    end

Example 1:

范例1:

=begin 
Ruby program to Calculate the factorial of 
given number using while loop
=end

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

i = 1
fact = 1

while i <= num  #implementation of while loop
	fact *= i
	i += 1
end

puts "The factorial of #{num} is #{fact}"

Output

输出量

Enter the number
5
The factorial of 5 is 120

Example 2:

范例2:

=begin 
Ruby program to check whether the given 
number is palindrome or not using while loop
=end

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

temp=num
sum = 0

while num!=0  #implementation of while loop
	rem=num%10
	num=num/10
	sum=sum*10+rem
end

if(temp==sum)
	puts "The #{temp} is a palindrome"
else
	puts "The #{temp} is not a palindrome"
end

Output

输出量

First run:
Enter the number
121
The 121 is a palindrome

Second run:
Enter the number
566
The 566 is not a palindrome

Example 3:

范例3:

=begin 
Ruby program to check whether the given number 
is Armstrong or not using while loop
=end

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

temp=num
sum = 0

while num!=0  #implementation of while loop
	rem=num%10
	num=num/10
	sum=sum+rem*rem*rem
end

if(temp==sum)
	puts "The #{temp} is Armstrong"
else
	puts "The #{temp} is not Armstrong"
end

Output

输出量

First run:
Enter the number
153
The 153 is Armstrong

Second run:
Enter the number
1
The 1 is Armstrong

Third run:
Enter the number
2332
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”的附加语句来停止循环。 通过以下示例可以证明这一点:

=begin 
Ruby program to make an infinite loop using while loop
=end

num=9

while num!=0  #implementation of while loop
	puts "Includehelp.com"
	break
end

Output

输出量

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值