Home »
Ruby Tutorial
Ruby Nested Do While Loop
By IncludeHelp Last updated : November 16, 2024
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.
Syntax
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: Count Armstrong Numbers Between Two Limits Using Nested do-while Loops
=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
if ul < ll
puts "Invalid input! Upper limit must be >= to the lower limit."
else
loop do
num = ul
temp = ul
arm = 0
# Inner loop to calculate the sum of cubes of digits
loop do
rem = num % 10
num = num / 10
arm = arm + rem * rem * rem
break if num == 0
end
# Check if the number is an Armstrong number
if temp == arm
puts "#{temp} is an Armstrong number"
end
ul -= 1 # Decrement the upper limit to move to the next number
break if ul < ll # Exit the loop when the range is complete
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: Count and Print Palindrome Numbers Between Two Limits Using Nested Loops
=begin
Ruby program to check and count the number of palindrome 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
if ul < ll
puts "Invalid input! Upper limit must be >= the lower limit."
else
palindrome_count = 0
loop do
num = ul
temp = ul
pal = 0
# Inner loop to reverse the digits of the number
loop do
rem = num % 10
num = num / 10
pal = pal * 10 + rem
break if num == 0
end
# Check if the number is a palindrome
if temp == pal
puts "#{temp} is a palindrome"
palindrome_count += 1
end
ul -= 1 # Decrement the upper limit to move to the next number
break if ul < ll # Exit the loop when the range is complete
end
puts "Total palindrome numbers found: #{palindrome_count}"
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.