What if you could tell your computer to do boring, repetitive work for you in just a few lines?
Why For loop in R Programming? - Purpose & Use Cases
Imagine you have a list of 100 numbers and you want to add 5 to each number one by one by writing each step manually.
Doing this by hand is slow, boring, and easy to make mistakes. You might forget a number or add the wrong value, and fixing errors takes a lot of time.
A for loop lets you tell the computer to repeat the same action for each item in your list automatically, saving time and avoiding errors.
result1 <- 1 + 5 result2 <- 2 + 5 result3 <- 3 + 5
for(i in 1:3) { print(i + 5) }
For loops let you handle repetitive tasks quickly and correctly, no matter how many items you have.
Think about sending a thank-you email to each person in a list of customers without writing each email separately.
Manual repetition is slow and error-prone.
For loops automate repeated actions efficiently.
They make your code shorter, clearer, and easier to manage.