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
-
First we initialize the value of i with 0.
-
Next we write the until loop with the condition
Which says, be in the loop until value of i becomes greater than 10.
-
Next we print the value of i.
-
And increment the value of i by 1.
-
Then when it finds the end statement, it goes back to
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
-
First we initialize the value of i with 0.
-
Next we write the begin statement, so that we go into the loop.
-
Next we print the value of i.
-
And increment the value of i by 1.
-
Then when it finds the end and until statement,
it checks if the condition has met or not. If not it continues to be in the loop until the condition is met.