We've seen how code usually runs from top to bottom, and how if
statements let our programs choose different paths. But what if we need to perform the exact same action multiple times?
Engagement Message
Can you think of a daily task you do that's very repetitive?
Imagine you wanted to print "Good morning!" three times. You could write console.log("Good morning!")
on three separate lines. But what if you needed to do it 50 times? That would be a lot of typing!
Engagement Message
How would you feel about writing the same line of code 50 times?
Programming provides a much better way to handle repetitive tasks. This is called iteration or looping. It's a way to tell the computer to repeat a set of instructions automatically.
Engagement Message
What do you think is the main advantage of this approach?
Using loops saves us effort and makes our code much shorter and cleaner. Instead of writing many identical lines, we write the instructions once and specify how many times they should be repeated.
Engagement Message
Why might shorter code be easier to understand and maintain?
In the upcoming units, we'll learn the specific ways to create these loops in JavaScript. The fundamental idea is to "perform these actions, then do them again, until a certain condition is met."
Engagement Message
Can you think of any real-world scenario that resembles this?
