Home » Ruby programming

Ruby nested do...while loop with examples

Nested do while loop in Ruby: In this tutorial, we are going to learn about the nested do while loop in Ruby programming language with syntax and examples.
Submitted by Hrithik Chandra Prasad, on August 01, 2019

Nested do...while loop

When two post-test loops i.e. do..while loops are working together for the fulfillment of a task, it is known as the nesting of the do...while loop. It, means that there are two do-while loops, first one is behaving as an outer loop and later one is acting as an inner loop. Execution of both the loops will take place in the way that first, the pointer will work with the outer loop then it will move to the inner loop as the condition is checked after the execution of the block. Again inside the block will be executed and then the condition will be checked and then the pointer will move to the outer loop for checking the specified Boolean condition of the outer loop. In this loop, both the blocks will be executed at least for once because do-while is an exit-control loop.

In Ruby, Nesting of the do...while loop can be done with the help of the following syntax:

    loop do
        # code block
        loop do
            # code block
            break if Condition
        end
        break if Condition
    end

Example 1:

=begin 
Ruby program to check the number of Armstrong numbers 
present between two limits using a nested do-while loop
=end

puts "Enter upper limit:-"
ul=gets.chomp.to_i
puts "Enter lower limit:-"
ll=gets.chomp.to_i

loop do
	num=ul
	temp=ul
	arm=0
	loop do
		rem=num%10
		num=num/10
		arm=arm+rem*rem*rem
		if num==0
			break
		end
	end
	if temp==arm
		puts "#{temp} is armstrong"
	end
	ul=ul-1
	if ul==ll
		break
	end
end

Output

Enter upper limit:-
1000
Enter lower limit:-
100
407 is armstrong
371 is armstrong
370 is armstrong
153 is armstrong

Example 2:

=begin 
Ruby program to check the number of numbers present 
between two limits using a nested do-while loop
=end

puts "Enter upper limit:-"
ul=gets.chomp.to_i
puts "Enter lower limit:-"
ll=gets.chomp.to_i

loop do
	num=ul
	temp=ul
	pal=0
	loop do
		rem=num%10
		num=num/10
		pal=pal*10+rem
		if num==0
			break
		end
	end
	if temp==pal
		puts "#{temp} is palindrome"
	end
	ul=ul-1
	if ul==ll
		break
	end
end

Output

Enter upper limit:-
200
Enter lower limit:-
100
191 is palindrome
181 is palindrome
171 is palindrome
161 is palindrome
151 is palindrome
141 is palindrome
131 is palindrome
121 is palindrome
111 is palindrome
101 is palindrome

In the above examples, you can observe that two do-while loops are working together to find the palindrome and Armstrong series between two ranges or limits. The outer do-while loop is working to decrease the upper limit by one so that the loop could work between those two specified numbers. The inner loop is processing each number to check whether it is palindrome/Armstrong or not.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.