What if you could tell your computer to do boring repetitive work for you in just a few lines of code?
Why For loops in MATLAB? - Purpose & Use Cases
Imagine you have a list of 100 numbers and you want to add 5 to each number one by one. Doing this by hand means changing each number separately, which takes a lot of time and effort.
Manually changing each item is slow and easy to mess up. You might forget to change some numbers or make mistakes in calculations. It's tiring and not practical for big lists.
For loops let you tell the computer to repeat the same action for each item automatically. You write the instructions once, and the loop does the work for every number, fast and without mistakes.
a = [1,2,3,4,5]; a(1) = a(1) + 5; a(2) = a(2) + 5; % and so on...
a = [1,2,3,4,5]; for i = 1:length(a) a(i) = a(i) + 5; end
For loops make it easy to repeat tasks on many items, saving time and avoiding errors.
Think about checking every item in a shopping list to see if it's in stock. A for loop can quickly check each item without you doing it one by one.
Manual repetition is slow and error-prone.
For loops automate repeated actions efficiently.
They help handle large sets of data easily.