Code highlights logo

Learn

Loops

Repeating Tasks Manually

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.

Example of Manual Repetition

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:

1console.log("Hello, World!");
2console.log("Hello, World!");
3console.log("Hello, World!");
4console.log("Hello, World!");
5console.log("Hello, World!");

Drawbacks of Manual Repetition

While repeating tasks manually is simple, it has several significant drawbacks:

  • Redundancy: The same code is written multiple times, leading to unnecessary duplication.
  • Maintenance: If you need to change the task (e.g., the message), you must update it in multiple places.
  • Scalability: If you need to repeat the task a different number of times, you must add or remove lines of code manually.
  • Readability: Large amounts of repeated code can make the program harder to read and understand.

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:

1for (let i = 0; i < 5; i++) {
2 console.log("Hello, World!");
3}

This concise code achieves the same result, demonstrating the power and efficiency of loops.


Instructions

1.

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?

Sign In

Course content