Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

Do While Loop in TypeScript

Learn how to use the do while loop in TypeScript, execute a block of code at least once, check conditions, and control repeated execution.

Simple Explanation: A do while loop executes the code first and checks the condition afterward. Therefore, the loop body executes at least once, even when the condition is initially false.

What Is a Do While Loop?

A do while loop is a control statement used to execute a block of code repeatedly while a specified condition remains true.

Unlike a while loop, the do while loop checks the condition after executing the loop body.

  • The loop body executes before the condition is checked.
  • The loop body executes at least once.
  • If the condition is true, the loop executes again.
  • If the condition is false, the loop terminates.

Do While Loop Workflow

Follow these steps to understand how a do while loop executes a block of code.

▶️
Step 1
Execute Code

Execute the loop body once.

→
🔍
Step 2
Check Condition

Evaluate the condition.

→
🔄
Step 3
Repeat or Stop

Repeat if true; stop if false.

1. Syntax of Do While Loop

The do while loop uses the do keyword followed by a block of code and a while condition.

Syntax

do {
    // Code to execute
} while (condition);

Important Points

  • The code inside the do block executes first.
  • The condition is checked after executing the code.
  • The semicolon after while (condition); is required.
Remember: A do while loop always executes its body at least once.

2. Write Your First Do While Loop

Let's create a program that prints numbers from 1 to 3 using a do while loop.

Example

let count: number = 1;

do {
    console.log(count);
    count++;
} while (count <= 3);

Expected Output

1
2
3

Explanation

  • count starts with the value 1.
  • console.log(count) displays the current value.
  • count++ increases the value by 1.
  • The loop stops when the condition becomes false.

3. When the Condition Is False

A do while loop executes once even if the condition is false before the first iteration.

Example

let count: number = 10;

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

Expected Output

10

Explanation

The loop prints 10 before checking the condition. After incrementing, count becomes 11. The condition count <= 5 is false, so the loop stops.

Key Point: This is the main difference between a while loop and a do while loop.

4. Calculate the Sum of Numbers

We can use a do while loop to calculate the sum of numbers from 1 to 5.

Example

let number: number = 1;
let sum: number = 0;

do {
    sum = sum + number;
    number++;
} while (number <= 5);

console.log("Sum:", sum);

Expected Output

Sum: 15

Explanation

The loop adds each number to the variable sum.

1 + 2 + 3 + 4 + 5 = 15

After adding 5, the value of number becomes 6. The condition is false, and the loop stops.

5. Do While Loop with an Array

A do while loop can also access array elements using their index positions.

Example

let courses: string[] = [
    "Java",
    "Python",
    ".NET"
];

let index: number = 0;

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

Expected Output

Java
Python
.NET

Explanation

  • The array stores three course names.
  • The index starts at 0.
  • Each iteration displays one course and increments the index.
  • The loop terminates when the index reaches the array length.

6. While Loop vs Do While Loop

While Loop Do While Loop
Checks the condition before execution. Checks the condition after execution.
May execute zero times. Executes at least once.
Condition is checked at the beginning. Condition is checked at the end.
while (condition) { } do { } while (condition);

When Should You Use a Do While Loop?

Use a do while loop when an operation must execute at least once before checking whether it should repeat.

Common Mistakes

  • Forgetting the semicolon after the while condition.
  • Forgetting to update the loop variable.
  • Writing an incorrect stopping condition.
  • Assuming the loop will skip execution when the condition is initially false.
  • Creating an infinite loop by using a condition that never becomes false.
Remember: Always check the loop condition and update the relevant variables to prevent an infinite loop.

Summary

In this lesson, we learned the syntax of a do while loop, how it executes before checking the condition, how to repeat statements, calculate a sum, and access array elements. We also compared the do while loop with the while loop and reviewed common mistakes.