Repeating tasks manually means executing the same piece of code multiple times by writing it out each time. This approach is straightforward but quickly becomes cumbersome as the number of repetitions increases.
Suppose we want to print the message "Hello, World!" five times. We could do this manually by writing the same line of code five times:
While repeating tasks manually is simple, it has several significant drawbacks:
As we'll explore in the upcoming lectures, loops provide a way to repeat tasks without manually writing out the code for each repetition. Here's a preview of how the above task could be done with a loop:
This concise code achieves the same result, demonstrating the power and efficiency of loops.
Let's print 3 time
"Hello" to the console. (using 3 console logs, one after another).
Sign up to start coding
Already have an account?