What if you could tell your computer to do all the boring, repetitive work for you in just a few lines?
Why For loop (rarely used in Ruby)? - Purpose & Use Cases
Imagine you have a list of tasks written on paper, and you want to check each one to see if it's done. Doing this by hand means looking at each task one by one, which takes time and can get confusing if the list is long.
Manually checking each item is slow and easy to mess up. You might skip a task or check the same one twice. It's tiring and not fun, especially when the list grows bigger.
The for loop in Ruby lets you tell the computer to go through each item in a list automatically. It does the boring, repetitive work for you, so you don't have to worry about missing anything.
puts tasks[0] puts tasks[1] puts tasks[2]
for task in tasks puts task end
It makes repeating actions over many items easy and error-free, freeing you to focus on what matters.
Think about sending a thank-you note to every friend who came to your party. Instead of writing each note separately, a for loop helps you send all messages quickly and correctly.
Manually repeating actions is slow and risky.
For loops automate repetition over lists.
They save time and reduce mistakes.