The problem loops solve
A program often has to do the same thing many times: print a message for every student, add up every price, check every row of a table. Without a way to repeat code, the only tool is to write the same statement again and again. A loop is the construct that runs a block of code repeatedly, and it is one of the most powerful tools in any programming language.
To see why that matters, this lesson first does the job the hard way.
Repeating by copy and paste
Imagine you have to print I promise to learn coding. five times. With only console.log, the program is five identical lines:
console.log("I promise to learn coding.");
console.log("I promise to learn coding.");
console.log("I promise to learn coding.");
console.log("I promise to learn coding.");
console.log("I promise to learn coding.");
Each call to console.log prints its text followed by a line break, so running the program shows the sentence on five separate lines. It works, and with five lines it is not even hard to write. Typing one line and copying it four times takes seconds.
Where copying breaks down
The trouble starts when the requirements change, and they always do:
- The count grows. If the teacher now wants the sentence 100 times, you need 100 lines. For 100,000 times, the source file is longer than anyone would want to read, let alone edit.
- The count is not known in advance. If the number of repetitions depends on the data, such as one greeting per name in an array, you cannot know how many lines to write when you write the program.
- Changes must be repeated. If the sentence needs a different wording, every copy has to be edited. Miss one and the output is inconsistent.
- Mistakes multiply. With a hundred pasted lines, it is easy to delete one or add one extra without noticing.
All of these have the same cause: the program says the same thing many times, and the number of times is written out as the number of lines. A better program would say it once and state how many repetitions it wants.
A first look at the loop version
TypeScript has two main loops for this, covered in the coming lessons. As a preview, here is the same job with a while loop:
let counter: number = 1;
while (counter <= 5) {
console.log("I promise to learn coding.");
counter++;
}
The sentence is written once. A counter starts at 1, the body runs as long as the counter is at most 5, and each pass increases the counter by one. It prints the same five lines. The part that changes the count is the number 5. Making it 100 or 100000 is a one-character edit, and changing the wording is a single edit as well.
You do not need to understand every detail yet. What matters is the shape: the repeated statement appears once, and a small piece of code around it controls how often it runs.
The exercise
The assignment is to print I promise to learn coding. five times using only console.log statements, which is the copy-and-paste way. The goal is to feel how repetitive it is, so that loops will feel like a relief in the next lessons.
- Write
console.log("I promise to learn coding.");once. - Copy it four more times, so the program has five identical lines.
- Check the text. The sentence ends with a full stop, and the lesson's tests compare the output exactly.
console.log("I promise to learn coding.");
console.log("I promise to learn coding.");
console.log("I promise to learn coding.");
console.log("I promise to learn coding.");
console.log("I promise to learn coding.");
Common mistakes
- Printing the wrong number of lines. Count them. Four or six lines both produce wrong output, and this is exactly the kind of slip that grows worse as the count gets larger.
- Changing the text slightly. A missing full stop or a different capital letter makes the output differ from what is expected.
- Printing it all in one call. One
console.logwith the sentence written five times prints a single long line, not five lines. - Forgetting the semicolon or a closing quote. A missing closing quote is a syntax error. A missing semicolon is accepted by TypeScript, but ending statements with one is the common style.
Key takeaways
- A loop runs the same code several times without copying it.
- Repeating by copy and paste does not scale: the number of lines grows with the number of repetitions.
- Copies are hard to change and easy to get wrong.
- With a loop, the repeated statement is written once, and the number of repetitions is a value you can change or compute.