Updated April 11, 2023
Introduction to Typescript for loop
In typescript, a for loop is defined as a control statement to execute a set of instructions or code for a given number of times in the for loop statement where it will be most recommended in array-like structures such as lists, arrays to iterate through the entire array or list and display one value at a time using the condition provided in the for a loop. In general, we can define it as an iterating statement as a condition declared within the loop to execute the code or statements within the for loop body with a specification to execute the code repeatedly such loop is known as for loop.
Syntax:
In Typescript, for loop has the same syntax as other programming languages:
for(initial_value, specific_condition, step_increment)
Parameters:
- intial_value: this parameter is used to specify the initial count value for the declared variable to start the iteration count and keep the track of iteration. Therefore, the code starts executing from this given initial count value in the “for” loop.
- specific_condition: this parameter is used to specify the logical condition where if this condition is satisfied then it executes the code within the loop.
- step_increment: this parameter is used to specify the count to repeat the set of code when the condition is satisfied which specifies how many numbers of times after the initial count specified must be repeated according to the given condition and the value of this parameter will be altered after every iteration.
This loop will use the return statement to break the loop and it is used to avoid the compile-time error and hence this loop returns the set of values or an array of values in which the code is executed within the “for” loop. In typescript, there are two other variations of “for” loop such as “for…of” loop and “for…in” loop which is mostly used for looping over the values in a set of items which can include array-like objects such as a set and map types in for…of and in for…in is used for looping over the object properties. The typescript also provides an array method known as foreach() where also works similar to the “for” loop but used to iterate through every item in the specified array.
Flowchart:
In the above, we can see a blueprint of how the “for” loop works in typescript along with the condition provided. This type of blueprint is known as a flowchart for the “for” loop which shows the pictorial representation of working or the process of flow of “for” loop in typescript which also briefs the algorithm of the any “for” loop code of any program.
How “for” loop works in Typescript?
How “for” loop works in Typescript along with demonstration with examples in the below section:
In typescript, the “for” loop works the same as in any other programming language. The “for” loop is a control flow statement that is used in iterations to execute a particular set of code that is written within the “for” loop and this loop first statement consists of 3 different expressions which are declared as in the first part it is used to define the count of iterations to start from which is initializes the variable and is executed only once; the second part is used to specify the condition to execute the code within the loop if the condition is satisfied ( if True the code is executed, else the loop does not enter the loop body) and if the condition fails then the “for” loop gets terminated, and the third part is used for declaring the count of increment of the iteration count which increments the initial count after every iteration that is specified in the “for” statement. The above condition is executed again and again, where the “for” loop continues to execute the set of code until the condition fails and the “for” loop comes out of the iteration that is specified and the loop gets terminated.
Examples
Now let us demonstrate the working of the “for” loop in the below example:
Example #1
console.log(" Demonstration of simple for loop in Typescript ")
console.log("The iteration number that for loop executes this statement are as follows ");
for(let n = 0; n <= 9; n++){
console.log(" The iteration number:"+ n);
}
Output:
In the above program, we have defined “for” loop by declaring a variable “n” where we have initialized value to this variable as “0” then we have specified a condition that says do the iteration starting from 0 to less or equal to 9 value of the variable “n” and after each iteration, the “n” value is incremented only once and each value is printed until it reaches the value till “9”. Therefore, in the above code, the output can be seen in the screenshot which prints the iteration numbers of how many times the “for” loop executes the statement within the loop and then gets terminated when it prints till the value of the variable to “9”.
Now let us consider another example in which the “for” loop is used to iterate each item in the array.
Example #2
console.log(" Demonstration of simple for loop to iterate through an array in Typescript ")
console.log("The items values that are specified in the given array is as follows ");
let arr1 = ["Educba", "Technology", "Institute", "Delhi", "India"];
for (let i = 0; i < arr1.length; i++) {
let arr_item = arr1[i];
console.log(arr_item);
}
Output:
In the above program, we can see we have first declared an array that contains 5 items and is assigned to the variable “arr1” then to print the items or elements in the array by iterating over each element we are using “for” loop where we are initializing the iteration count from 1 which indicates the index number in the given array starting from index 0 we are printing the items till the end of the array and this is done by using array method known as length() which will take the items until the given array length and here the array length is 5. To print each element, we are declaring the array along with its index such as arr1[i].
Conclusion
In this article, we conclude that the “for” loop is a control statement for printing the repeated items by traversing through the given set. In this article, we can see there are for…in and for..of loops which are variations of for loop similar to the foreach loop. These also can traverse through a set of items for looping over the values and looping over the object properties.
Recommended Articles
We hope that this EDUCBA information on “Typescript for loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.