# Ruby nested until loop with examples

**Nested until loop in Ruby**: In this tutorial, we are going to learn about the **nested until loop in Ruby programming language with syntax and examples**.

Submitted by Hrithik Chandra Prasad, on August 05, 2019

## Nested until loop

Alike for, while, and do...while, until loop can also be nested for meeting the specific purpose. In this type of nesting, two until loops work in the combination such that at first, the outer loop is triggered which results in the execution of the inner loop. The outer Until loop is not invoked as long as the inner loop does not comes out to be true. It is a kind of Entry control loop which simply means that first the outer Boolean expression is processed; if it is false then the pointer will move to the inner loop to check inner condition. The whole execution will take place in the same manner.

**Syntax:**

until conditional [do] until conditional [do] # code to be executed end # code to be executed end

**Example 1:**

=begin Ruby program to find the sum of numbers lying between two limits using nested until loop =end puts "Enter the Upper limit" ul=gets.chomp.to_i puts "Enter the Lower limit" ll=gets.chomp.to_i until ul==ll num=ul temp=ul sum = 0 until num==0 #implementation of until loop rem=num%10 num=num/10 sum=sum+rem end puts "The sum of #{temp} is #{sum}" ul=ul-1 end

**Output**

Enter the Upper limit 1000 Enter the Lower limit 980 The sum of 1000 is 1 The sum of 999 is 27 The sum of 998 is 26 The sum of 997 is 25 The sum of 996 is 24 The sum of 995 is 23 The sum of 994 is 22 The sum of 993 is 21 The sum of 992 is 20 The sum of 991 is 19 The sum of 990 is 18 The sum of 989 is 26 The sum of 988 is 25 The sum of 987 is 24 The sum of 986 is 23 The sum of 985 is 22 The sum of 984 is 21 The sum of 983 is 20 The sum of 982 is 19 The sum of 981 is 18

**Example 2:**

Pattern printing: Print the following pattern

0 01 012 0123 01234

**Code:**

=begin Ruby program to print the given pattern. =end num=0 until (num==6) j=0 until(j==num) print j j+=1 end puts "" num+=1 end

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

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