TypeScript do-while Loop

Introduction

In this chapter, we will explore the do-while loop in TypeScript. The do-while loop is a control flow statement that allows you to execute a block of code at least once, and then repeatedly execute the block as long as a specified condition is true. Understanding how to use the do-while loop is essential for managing loops that need to execute at least once before checking a condition in TypeScript programs.

Table of Contents

  • Definition
  • Do-While Loop Syntax
  • Basic Do-While Loop
  • Using Do-While Loop with Arrays
  • Nested Do-While Loop
  • Complete Example with Output
  • Conclusion

Definition

The do-while loop repeats a block of code at least once, and then continues to repeat as long as a specified condition is true. This loop is useful when you need the code to run at least once regardless of the condition.

Do-While Loop Syntax

Syntax

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

Example

This example demonstrates a basic do-while loop that prints numbers from 0 to 4.

let i: number = 0;

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

Output

0
1
2
3
4

Basic Do-While Loop

The basic do-while loop ensures that the code block is executed at least once before the condition is tested.

Example

This example prints the first five even numbers using a do-while loop.

let num: number = 0;

do {
  console.log(num);
  num += 2;
} while (num < 10);

Output

0
2
4
6
8

Using Do-While Loop with Arrays

The do-while loop can be used to iterate over the elements of an array.

Example

This example iterates over an array of colors and prints each one using a do-while loop.

let colors: string[] = ["red", "green", "blue"];
let index: number = 0;

do {
  console.log(colors[index]);
  index++;
} while (index < colors.length);

Output

red
green
blue

Nested Do-While Loop

A nested do-while loop is a do-while loop inside another do-while loop. It allows for more complex iterations.

Example

This example prints a multiplication table for numbers 1 through 3 using nested do-while loops.

let i: number = 1;

do {
  let j: number = 1;
  do {
    console.log(`${i} * ${j} = ${i * j}`);
    j++;
  } while (j <= 3);
  i++;
} while (i <= 3);

Output

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9

Complete Example with Output

In this section, we will combine all the examples into a single TypeScript file, compile it to JavaScript, and run it to see the output.

TypeScript Code

You can test the following code in the TypeScript Playground:

// Basic Do-While Loop
let i: number = 0;

do {
  console.log(i); // Output: 0, 1, 2, 3, 4
  i++;
} while (i < 5);

// Using Do-While Loop with Arrays
let colors: string[] = ["red", "green", "blue"];
let index: number = 0;

do {
  console.log(colors[index]); // Output: red, green, blue
  index++;
} while (index < colors.length);

// Nested Do-While Loop
i = 1;

do {
  let j: number = 1;
  do {
    console.log(`${i} * ${j} = ${i * j}`); // Output: 1 * 1 = 1, 1 * 2 = 2, ... , 3 * 3 = 9
    j++;
  } while (j <= 3);
  i++;
} while (i <= 3);

Conclusion

In this chapter, we covered the do-while loop in TypeScript, including the basic do-while loop, using do-while loop with arrays, and nested do-while loops. We provided a complete example with its output to illustrate how these loops work in TypeScript. Understanding the do-while loop is essential for managing loops that need to execute at least once before checking a condition in TypeScript programs.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare