We are going to see the use of break statement to get out of a loop.
So, we go into the loop first. Then we specify the condition part in an if statement.
And place a break inside the if statement.
i=1 loop do puts i; i = i+1; if i > 10 break end end
loop do
puts i;
i = i+1;
if i > 10 break end
So, it is the break statement that is causing the control to come out of the loop.
next is used inside a loop to skip that particular iteration.
for i in 1..10 do if i > 5 next end puts i end
So, in the above programme, only the values from 1 to 5 will be printed. As in the if block we have specified the next statement,
if i > 5 next end
So, whenever the value of i is more than 5. The next statement is executed inside the if block. Which is causing to skip the current iteration.