Home »
Ruby Tutorial
Ruby nested while loop with examples
By IncludeHelp Last updated : November 16, 2024
Nested While Loop
When one while loop is living inside another while loop, it is known as nesting of while loop. It means that there are two while loops, the first one is acting as an outer loop and later one is behaving as the inner loop. Execution will take place in the manner that first the outer ‘while’ loop is triggered, then if the specified Boolean condition is matched the pointer will be passed to inner ‘while’ loop. In this too, if the Boolean condition stands to be true, the inner while loop body will be executed until specified condition does not come out to be false. Once the inner loop completes its execution, the pointer will be passed back to the outer for loop for its successful execution.
Syntax
In Ruby, Nesting of the while loop can be done with the help of the following syntax:
while (condition )
while (condition )
# code to be executed
end
#expressions
end
Example 1: Print a Pattern Using Nested while Loop
We can print various patterns using nested while loops. Let us see how the following pattern can be printed.
=begin
Ruby program to print a pattern using nested while loop
=end
num = 0
while num != 6
j = 0
while j != num
print num # Print the current value of `num` without a newline
j += 1 # Increment the inner loop counter
end
puts "" # Move to the next line after the inner loop finishes
num += 1 # Increment the outer loop counter
end
Output
1
22
333
4444
55555
Example 2: Count Palindrome Numbers Between Two Limits Using Nested while Loop
=begin
Ruby program to check the number of palindrome numbers
present between two limits using nested 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
while ul >= ll
num = ul
temp = ul
pal = 0
# Inner while loop to reverse the number
while num != 0
rem = num % 10
num = num / 10
pal = pal * 10 + rem
end
# Check if the number is a palindrome
if temp == pal
puts "#{temp} is a palindrome"
end
ul -= 1 # Decrement the upper limit to check the next number
end
end
Output
Enter upper limit:-
200
Enter lower limit:-
10
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
99 is palindrome
88 is palindrome
77 is palindrome
66 is palindrome
55 is palindrome
44 is palindrome
33 is palindrome
22 is palindrome
11 is palindrome
You can observe in the above program that first the outer while loop is checked through a specified condition i.e. while loop will run until upper limit does not become equal to the lower limit.
The upper limit is passed as the number for which Palindrome check will be carried out using an inner while loop. The inner while loop has all those statements which are necessary to carry out the checking. Once, the inner evaluation is done. The pointer is going back to the outer loop and the upper limit is getting decreased by 1.