For Loop - Repetition Structures in Javascript

Опубликовано: 14 Март 2026
на канале: Celso Kitamura | Mentor de Programadores
6,402
424

There are situations where we need to execute certain lines of code multiple times for our software to function.

In this video, we'll begin to look at repetition structures.

And today we'll see the first one, the for loop, one of the repetition structures in Javascript.

For Loop – Repetition Structures in Javascript

Loops offer an easy and quick way to execute an action repeatedly.

We can do this through repetition structures.

Repetition structures prevent the programmer from writing the same code multiple times when the execution flow is repetitive in nature.

A for loop consists of three variables: initialization, condition, and step.

In the initialization, we set the initial value of the repetition.

The condition is a Boolean expression (that returns true or false) that decides whether the repetition continues.

And in the step, we define the interval of the repetition.

Syntax

for (initialization variable; condition; step)
{

/execute this code block until the condition is met

}

Let's See an Example?

for (var i = 0; i [less than] 10; i++)
{
console.log("Value of i: " + i);

}

In this example, the numbers from 0 to 9 will be printed on the screen.

Let's understand how it works?

Follow along in the code:

1. We declare the variable i and initialize it with 0 var i = 0

2. Checking the condition
i [less than] 10

3. Since the condition is true (0 is less than 10), the code block is executed
console.log("Value of i: " + i);

This command prints "Value of i: 0" to the screen.

4. The variable i is incremented with ++ (at this point, 1 is added to the value of i, that is: 0 + 1 = 1)

5. And the condition is checked again:
i [less than] 10

6. At this point, i equals 1, so the inner code block is executed again and we will have on the screen:
Value of i: 1

7. The variable i is incremented again with ++ (now i equals 2, because 1 + 1 = 2)

8. This repeats until i reaches the value 10.

9. At this point, the condition i [less than] 10 will be false (because 10 is not less than 10) and the execution of the inner block of the for loop structure is canceled.

We can define the initialization, condition, and step in different ways so that the program behaves the way we want.

To print only the even numbers in the previous example, we can do this:

for (var i = 0; i [less than] 10; i = i + 2)

{ console.log("Value of i: " + i);

}

This code prints the numbers 0, 2, 4, 6, 8 to the screen.

Challenge

Now it's your turn to practice the for loop, to practice the repetition structure in Javascript.

Write a program that prints the numbers from 0 to 10 in descending order. Use the first example as a base.

Final Words

Repetition structures in Javascript are codes that repeat the execution of a portion of code until a certain condition is met.

Just like decision structures, there is more than one way to build these structures. We will see more in other videos in this series.

And that's all for today!

And if you want to learn more about programming, keep following me!

If you enjoyed this video, give it a like and subscribe to the channel. Don't forget to turn on notifications to receive an alert when new videos are published.

See you next time!

Follow me here ⤵⤵

💻 Blog: https://celsokitamura.com.br
💻 Instagram:   / celsokitamura  
🎥 YouTube:    / celsokitamura  
🔵 Facebook:   / celsokitamura  

#LoopForRepetitionStructuresInJavascript
#IAmAnAppDev