Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - UNTIL LOOP


Until Loop is the opposite of while loop. i.e. It comes out of the loop when the condition becomes true.


Let us have a look at the below example which says, be in the loop until value of i becomes greater than 10:


Example of 'until' loop with 'until' statement at the beginning of block:


Example :



i = 0;

until i > 10  do
   puts i;
   i = i+1;
end


  1. First we initialize the value of i with 0.

  2. Next we write the until loop with the condition

    until i > 10  do


    Which says, be in the loop until value of i becomes greater than 10.

  3. Next we print the value of i.

    puts i;

  4. And increment the value of i by 1.

    i = i+1;

  5. Then when it finds the end statement, it goes back to

    until i > 10  do


    and checks if the condition has met or not. If not it continues to be in the loop until the condition is met.

Example of 'until' loop with 'until' statement at the end of block:


This loop will execute the code block at least once, because it gets into the loop first, executes the block inside the loop. Then it checks for the conditional part i.e. until num<=10.


Example :



i = 0;

begin
	puts i;
	i = i+1;
end until i > 10


  1. First we initialize the value of i with 0.

  2. Next we write the begin statement, so that we go into the loop.

    begin

  3. Next we print the value of i.

    puts i;

  4. And increment the value of i by 1.

    i = i+1;

  5. Then when it finds the end and until statement,

    end until i > 10  do


    it checks if the condition has met or not. If not it continues to be in the loop until the condition is met.