Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
`for` loop | Loops
Introduction to TypeScript
course content

Contenido del Curso

Introduction to TypeScript

Introduction to TypeScript

1. TypeScript Fundamentals
2. Conditional Statements
3. Arrays
4. Loops
5. Functions

book`for` loop

Let's move on to the most commonly used type of loop - the for loop. The for loop is a loop that performs a specified number of operations set by the programmer. Its main feature is that inside the loop, a separate variable is created that stores the iteration number, as well as the condition for stopping the loop and the operation on the created variable. Let's take a look at the syntax:

In this syntax:

  • initialization is executed once before the loop begins and is typically used to initialize a counter;
  • condition is the condition that is checked before each iteration. If the condition is true, the loop continues to execute; if it's false, the loop terminates;
  • increment/decrement is executed after each iteration and is typically used to increase or decrease the value of the counter.

Example usage of a for loop:

123
for (let i = 1; i <= 5; i++) { console.log(`Iteration number ${i}`); }
copy

Let's go through everything that's been written and understand what's what:

  • let i = 0; – this is initialization. This is the variable that is created when we enter the loop and is deleted when the loop has completed all the necessary iterations. i is an index, simply because programmers are lazy to write the whole word. You can give this variable any name you like. This index is responsible for which iteration it is in the sequence;
  • i <= 5; – this is the condition. It is the limit to which the number of iterations will increase. During each iteration, the variable i will increase until it reaches the limit. In our case, the limit is 5. Once i takes the value of 5, the loop will terminate;
  • i++ – This is an increment. This is how much we will increase the i variable with each iteration of the loop. Different operations can be used here. For example, i-- or even i = i * 3. Any operation;
  • console.log(`Iteration number ${i}`); – this is the operation that the loop will perform. In our case, we are outputting the value of the variable i to see which iteration is currently being executed.

Let's look at another example of using the for loop to solidify the syntax in memory:

123
for (let i:number = 10; i <= 25; i = i + 5) { console.log(`The value of 'i' is ${i}`); }
copy

Now we have set slightly different conditions for changing the variable i. Initially, it is assigned the value 10, and then it is increased by 5 each time until it reaches the value of 25.

Why do we need this?

To precisely determine the number of iterations and set special conditions for the iterations.

Let's imagine that you suddenly have an urge to calculate the sum of all numbers from 1 to 100. Doing this manually takes a long time, just like using a calculator. That's why I suggest writing a quick loop that will do it for us:

1234567
let res: number = 0; // result variable for (let i: number = 1; i <= 100; i++) { // i <= 100 res += i; } console.log(res); // printing result
copy

In the code above, we initialize a variable that stores our result. Then, within the loop, we initialize our variable i. Initially, i is set to 1, and the variable i represents the number we will add with each iteration. This continues until we add the number 100. After that, the loop stops.

Pay attention to the operation we perform on the res variable. We use res += i;, which is equivalent to res = res + i;. This allows us to simplify any reassignment. For example, if we want to multiply, we can use res *= i instead of res = res * i.

Now you know that the sum of all numbers from 1 to 100 is 5050. I don't know where you'll use this information, but it's useful!

1. What does the `for` loop in TypeScript primarily help with?
2. Which part of a `for` loop in TypeScript specifies the condition for continuing the loop?
What does the `for` loop in TypeScript primarily help with?

What does the for loop in TypeScript primarily help with?

Selecciona la respuesta correcta

Which part of a `for` loop in TypeScript specifies the condition for continuing the loop?

Which part of a for loop in TypeScript specifies the condition for continuing the loop?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt