+ 14
Can anyone explain me for loops in Ruby
4 Answers
+ 16
Try to repeat SoloLearn's Ruby course, chapter Control Structures > For loops.
If you have further questions, please tell us exactly what your problem is.
+ 13
thanks
+ 7
A for loop is like a while loop spot the difference here:
while loop:
x = 0
while x < 10
x += 1
puts x
end
for loop:
x = 0
for x in 100
x += 1
puts x
end
I'm not sure to my answer but I think this is right
+ 6
@MrCoder there are other differences, as for iterates over something. e.g. you can say
for i in (5..100)
puts i
end
this puts out the numbers 5-100 because it iterates over the range (5..100)
you can also iterate over an array, for example
arr = ["ThingA", "ThingB", "ThingC"]
for i in arr
puts i
end
will output:
ThingA
ThingB
ThingC
basically, for reassigns i to the next element of the array/range/hash/whatever each time the loop repeats. when it reaches the end, it stops.