Programming Language
TypeScript
Loops

TypeScript - Loops

Introduction

A loop is a programming structure that repeats a block of code multiple times. Loops are essential for performing repetitive tasks and automating processes, making code more efficient. In TypeScript, there are several types of loops, including the for, while, and do...while loops.


For Loop

The for loop is one of the most commonly used loops. It is used when the number of iterations is known before entering the loop.

Syntax:

for (initialization; condition; increment/decrement) {
  // Code to be executed for each iteration
}
  • initialization: Defines the initial state of the loop variable.
  • condition: Specifies the condition to test before each iteration.
  • increment/decrement: Defines how the loop variable changes after each iteration.
for (let i = 0; i < 5; i++) {
  console.log(i);
}

While Loop

The while loop repeatedly executes a block of code as long as the specified condition is true. If the condition is false initially, the code inside the loop will not execute.

while (condition) {
  // Code to be executed while condition is true
}

condition: The condition is evaluated before each iteration. If it’s true, the loop continues.

let i = 0;
 
while (i < 5) {
  console.log(i);
  i++;
}

Do While Loop

The do...while loop is similar to the while loop, except that it always executes the block of code at least once, regardless of the condition. After executing the code, the condition is checked, and if it is true, the loop continues.

do {
  // Code to be executed
} while (condition);

The condition is checked after the block of code is executed.

Example:

let i = 0;
 
do {
  console.log(i);
  i++;
} while (i < 5);

Infinite Loop

An infinite loop is a loop that never ends unless manually interrupted. This happens when the condition is always true or when the loop does not have an exit condition.

let i = 0;
 
while (true) {
  console.log(i);
  i++;
  if (i >= 5) {
    break; // Exit condition to stop the infinite loop
  }
}

Break Statement

The break statement is used to exit a loop immediately, regardless of the condition. It is often used with conditional statements to terminate loops early.

for (let i = 0; i < 10; i++) {
  if (i === 5) {
    break;
  }
  console.log(i);
}

Continue Statement

The continue statement skips the current iteration of the loop and moves to the next iteration. It is commonly used to skip unwanted conditions.

for (let i = 0; i < 5; i++) {
  if (i === 2) {
    continue;
  }
  console.log(i);
}

Comparison of Loops

Loop TypeBest Used WhenExecutes At Least Once?
ForNumber of iterations is knownNo
WhileCondition is evaluated before the loop startsNo
Do WhileBlock must execute at least once before conditionYes

Conclusion

Loops in TypeScript are crucial for efficient programming and automation. Here's a quick guide:

  • Use a for loop for a predefined number of iterations.
  • Use a while loop when the loop depends on a condition being true.
  • Use a do...while loop when you need at least one execution before the condition is checked.
  • Use break to exit a loop prematurely.
  • Use continue to skip specific iterations.