Зміст курсу
Introduction to TypeScript
Introduction to TypeScript
`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 istrue
, the loop continues to execute; if it'sfalse
, 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:
for (let i = 1; i <= 5; i++) { console.log(`Iteration number ${i}`); }
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 variablei
will increase until it reaches the limit. In our case, the limit is 5. Oncei
takes the value of 5, the loop will terminate;i++
– This is an increment. This is how much we will increase thei
variable with each iteration of the loop. Different operations can be used here. For example,i--
or eveni = 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 variablei
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:
for (let i:number = 10; i <= 25; i = i + 5) { console.log(`The value of 'i' is ${i}`); }
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:
let res: number = 0; // result variable for (let i: number = 1; i <= 100; i++) { // i <= 100 res += i; } console.log(res); // printing result
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!
Дякуємо за ваш відгук!