Home » Ruby programming

Ruby while loop with examples

while loop in Ruby: In this tutorial, we are going to learn about the while loop in Ruby programming with its syntax, examples and the concept of Infinite while loop.
Submitted by Hrithik Chandra Prasad, on July 31, 2019

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:

  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.

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

    while (Boolean condition )
        # code to be executed
    end

Example 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:

=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:

=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

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:

=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.

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.

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.