The while loop is a control flow statement in TypeScript that repeatedly executes a block of code as long as a specified condition is true.
Example:
let count = 0;
while (count < 5) {
console.log(`Count is ${count}`);
count++;
}
Do...While Loop:
The do...while loop is similar to the while loop, but it ensures that the code block is executed at least once before checking the condition for repetition.
Example:
let num = 1;
do {
console.log(`Number is ${num}`);
num++;
} while (num <= 5);
For Loop:
The for loop is a compact control structure in TypeScript used for iterating over a range of values. It consists of an initialization, a condition, and an increment/decrement expression.
Example:
for (let i = 0; i < 5; i++) {
console.log(`Iteration ${i}`);
}
For...In Statement:
The for...in statement in TypeScript is used to iterate over the enumerable properties of an object. It is typically used for iterating over object keys or array indices.
Example:
const person = { name: "John", age: 30, city: "New York" };
for (const key in person) {
console.log(`${key}: ${person[key]}`);
}
For...Of Statement:
The for...of statement is used to iterate over the values of iterable objects such as arrays, strings, and collections. It simplifies the process of looping through elements.
Example:
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(`Color: ${color}`);
}