For loops are used in Ruby to loop over a set of elements. Unlike a while loop, which can lead to an infinite loop if we're not careful, for loops have a definite end because they loop over a finite amount of components.
FOR LOOPS and WHILE LOOPS are the two most common loop types. While a For loop runs a fixed number of times, a While loop runs a variable number of times. When you know how many times you want to run an algorithm before stopping, you use for loops.
Share a personalized message with your friends.