Loops in TypeScript
We offer you a brighter future with industry-ready online courses - Start Now!!
In TypeScript, loops are a way to execute a code block repeatedly until a specific condition is met. There are three main loops in TypeScript: the for loop, the while loop, and the do-while loop.
The for loop is used when you know the exact number of times you want to loop. It consists of three parts: the initialization, the condition, and the iteration. The initialization is done only once at the beginning, the state is checked before each iteration, and the iteration is executed at the end of each iteration.
The while loop is used when you want to loop while a specific condition is true. The state is checked before each iteration, and if it is true, the code block is executed. If it is false, the loop ends.
The do-while loop is similar to the while loop, but the condition is checked at the end of each iteration instead of the beginning. This means that the code block is executed at least once, even if the state is false from the start.
Loops are a powerful tool in TypeScript that allows you to automate repetitive tasks and process large amounts of data efficiently. However, it is essential to be careful when using loops to avoid infinite loops or other unintended consequences.
Definite and Indefinite Loops
In TypeScript, loops can be categorized as definite or indefinite, depending on whether the number of iterations is known.
A definite loop is a loop where the number of iterations is known beforehand. The most common example of a definite loop is the for loop, where the number of iterations is specified in the loop’s initialization step. Definite loops are helpful when you know how many times you need to execute a code block and want to optimize the loop’s performance.
An indefinite loop is a loop where the number of iterations is unknown beforehand. The most common example of an indefinite loop is the while loop, which continues as long as a specific condition is true. Indefinite loops are helpful when you don’t know in advance how many times you need to execute a block of code or when you want to keep looping until a certain condition is met.
It is essential to be careful when using indefinite loops to avoid infinite loops or other unintended consequences. Infinite loops occur when the loop condition is never false, causing the loop to execute indefinitely and potentially crashing your program. To prevent this, it’s essential to ensure that the loop condition eventually becomes false or that there is a way to break out if needed.
For Loops in TypeScript
A for loop is used to run a block of code repeatedly. It is frequently used to loop through an array or other objects. Similar to JavaScript’s for loop, a for loop in TypeScript has the following syntax:
for (initialization; condition; increment) {
// code to be repeated
}
The initialization statement, which is used to establish a variable’s initial value, is run before the loop begins. Before beginning each iteration of the loop, the condition statement is assessed to determine if the loop should continue. Each iteration ends with the increment statement being executed. The value of the variable used in the condition statement is usually updated using it.
Here’s an example of a for loop in TypeScript that iterates over an array of numbers and calculates their sum:
const DataFlair_numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < DataFlair_numbers.length; i++) {
sum += DataFlair_numbers[i];
}
console.log(sum);
Output
In this illustration, the loop begins with i= 0 and runs until I fall below the length of the DataFlair_numbers array. The sum variable is increased with the value of the current array element on each iteration.
Types of For Loop
In TypeScript, the for loop is a type of loop that allows you to execute a block of code a specified number of times. The for loop has three parts: the initialization, the condition, and the iteration.
There are two types of for loops in TypeScript: the traditional for loop and the for…of the loop.
The traditional loop is used when you know the exact number of times you want to loop. It consists of three parts: the initialization, the condition, and the iteration. The initialization is done only once at the beginning, the condition is checked before each iteration, and the iteration is executed at the end of each iteration. Here is an example:
for (let DataFlair_i = 0; DataFlair_i < 10; DataFlair_i ++) {
console.log(DataFlair_i );
}
Output –
1
2
3
4
5
6
7
8
9
This loop will execute 10 times and print the numbers from 0 to 9 to the console.
The for…of the loop is used when you want to loop over the elements of an array or an iterable object. It was introduced in ECMAScript 6 and is supported in TypeScript. Here is an example:
const DataFlair_arr = [1, 2, 3, 4, 5];
for (const item of DataFlair_arr) {
console.log(item);
}
Output–
2
3
4
5
This loop will iterate over each element of the array arr and print it to the console.
Both types of for loops are useful in different situations, and it’s important to choose the right type of loop for your needs. The traditional for loop is best when you know the exact number of iterations, while the for…of loop is best when you need to iterate over the elements of an array or an iterable object.
While Loops in TypeScript
While a specific condition is true, a code block is repeated using a while loop. Following syntax:
while (condition) {
// code to be repeated
}
The condition statement is evaluated before each iteration of the loop and determines whether the loop should continue or not.
Here’s an example of a while loop in TypeScript that generates random numbers until a number greater than 0.5 is generated:
let DataFlair_randomNumber = Math.random();
while (DataFlair_randomNumber <= 0.5) {
console.log(DataFlair_randomNumber);
DataFlair_randomNumber = Math.random();
}
console.log(DataFlair_randomNumber);
In this illustration, the loop begins with a number that is chosen at random and runs until it reaches a value of less than or equal to 0.5. A new random number is created and verified after each iteration.
Do-While Loops in TypeScript
Like a while loop, a do-while loop executes the code block at least once before checking the condition. A do-while loop in TypeScript has the following syntax:
do {
// code to be repeated
} while (condition);
Here’s an example of a do-while loop in TypeScript that asks the user to enter a number until a positive number is entered:
let DataFlair_number;
do {
DataFlair_number = prompt('Enter a positive number:');
} while (DataFlair_number <= 0);
console.log(DataFlair_number);
Output
In this instance, the loop asks the user to enter a DataFlair_number at the outset. The loop will continue if the joined DataFlair_number is less than or equal to 0. A positive DataFlair_number causes the loop to end, and the entered DataFlair_number to be shown.
Loop Control Statements
In addition to the basic loop types, TypeScript also supports several loop control statements that allow you to control the flow of a loop.
Break
Use the break statement to end a loop right away. When it is run, the loop is broken, and the program moves on to the code following the loop. Here is an illustration of how to use a for loop’s break statement to end the loop when a specific condition is satisfied:
const DataFlair_numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < DataFlair_numbers.length; i++) {
if (sum >= 10) {
break;
}
sum += DataFlair_numbers[i];
}
console.log(sum);
Output
The loop in this illustration adds up an array of numbers iteratively until the total is more than or equal to 10. When this criterion is satisfied, the break statement is carried out, which ends the loop.
Continue
Use the continue statement to continue with the next loop iteration while skipping the current one. When it is run, the loop skips the remaining words and moves on to the following iteration. Here is an illustration of how to skip even numbers in a for loop using the continue statement:
const DataFlair_numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < DataFlair_numbers.length; i++) {
if (DataFlair_numbers[i] % 2 === 0) {
continue;
}
sum += DataFlair_numbers[i];
}
console.log(sum);
Output
In this example, the loop iterates over an array of numbers and skips even numbers using the continue statement.
Conclusion
Programming requires loops, and TypeScript offers a variety of methods to employ them. While the while and do-while loops repeat a block of code as long as a condition is true, the for loop iterates through a set of elements. Moreover, TypeScript offers loop control statements like break and continue, which let you manage a loop’s progress. Create TypeScript code that is more effective and manageable by utilizing loops and control statements.
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

