Locked lesson.
About this lesson
Understanding for and each loops, and when to use them.
Exercise files
Download this lesson’s related exercise files.
For and Each Loops.docx60.1 KB For and Each Loops - Solution.docx
59.1 KB
Quick reference
For and Each Loops
Let's learn the for and each loops.
When to use
As with most loops, using them is largely a personal preference.
Instructions
To create a for loop:
for number in 0..10
puts number
end
To create an each loop:
(0..10).each do |number|
puts number
end
Hints & tips
- Use for loops to loop within a range
- Use each loops to loop over each item
Lesson notes are only available for subscribers.