create a loop in ruby

In Ruby, there are several ways to create loops, but the two most common are the while loop and the for loop.

main.rb
# While loop
i = 0
while i < 10 do
  puts i
  i += 1
end
57 chars
7 lines

In the example above, we initiated a variable i and created a while loop that loops until i is less than 10. During each iteration, we output the current value of i and increment it by one.

main.rb
# For loop
for i in 0..9
  puts i
end
38 chars
5 lines

In the example above, we used a for loop to iterate over a range of numbers from 0 to 9 (inclusive). During each iteration, we output the current value of i.

Both loops can be used to perform tasks repeatedly until a certain condition is met. The choice of which loop to use will depend on the specific task that you are trying to perform.

gistlibby LogSnag